Tuesday, 14 March 2017

Name overwriting when multiple sessions in my Node Chat

I am creating a node chat - and I am having a problem with my users. When i log on with different usernames they all change to the username that joined last.

How do i solve this problem?

Routes.js:

module.exports = (function (app) {
var User = require('mongoose').model('user');
var Room = require('mongoose').model('room');
var Schema = require('../models/schema');

var userNames = [];
app.locals.userNames = userNames;

app.get('/', function(req, res) {
    res.render("index");
});

app.get("/create", function(req, res){
    res.render("create");
});

app.get("/delete", function(req, res){
    Schema.userSchema.find({rooms:req.params["rooms"]}, function (err, messages) {
        if (err) {
            console.log(err);
            res.send(err);
        }
        Schema.roomSchema.find({}, function (err, roomName) {
            console.log(messages);
            if (err) {
                console.log(err);
                res.send(err);
            }
            res.render("delete", {
                allMessages: messages,
                allRooms: roomName
            });
        });
    });
});

app.get("/:rooms", function(req, res) {
    Schema.userSchema.find({rooms:req.params["rooms"]}, function (err, messages) {
        if (err) {
            console.log(err);
            res.send(err);
        }
        Schema.roomSchema.find({}, function (err, roomName) {
            console.log(messages);
            if (err) {
                console.log(err);
                res.send(err);
            }
            res.render("rooms", {
                allMessages: messages,
                allRooms: roomName
            });
        });
    });
});


app.post("/", function(req, res) {

    createUser = 0;
    if (!req.body.userName) {
        res.status(400).send("You must fill in a username");
        return;
    }
    else {
        for(i = 0; i < userNames.length; i++) {
            if (userNames[i]["userName"] == req.body.userName) {
                createUser = 1;
                break;
            }
        }
    }
    if (createUser == 0) {
        req.session.users = req.body.userName;
        userNames.push({userName: req.body.userName,});
        res.redirect("/rooms");
    }
    else {
        res.status(400).send("Name is already used");
        return;
    }
});
//Create room
app.post("/create", function(req, res) {
    if (!req.body.title) {
        res.status(400).send("You must give your chatroom a name");
        return;
    }
    var newRoom = new Room({title: req.body.title});

    newRoom.save((err) => {
        if (err) {
            return next(err);
        } else {
        }
    });
    res.redirect("/rooms");
});

//Delete room
app.post("/delete", function(req, res) {
    Schema.roomSchema.remove({_title: req.params.title}, function (err) {
        if (!err) {
            console.log("deleted");
        }
        else {
            console.log("error");
        }
    });
    res.redirect("/rooms");
});


app.post("/:rooms", function(req, res) {
    if (!req.body.message) {
        res.status(400).send("You must enter a message");
        return;
    }
    var user = new User({users: req.session.users, messages: req.body.message, rooms: req.params["rooms"]});

    user.save((err) => {
        if (err) {
            return next(err);
        } else {
        }
    });
    res.redirect(req.params["rooms"]);
});

Feel free to give me pointers for improvement.



via MrBawsEnough

No comments:

Post a Comment