0

I am using sockets with mongodb, for a user who is trying to create a new name, I need to check all the models in the database to see if it exists.

I am doing it all wrong, basically I am trying to do something like this.

var allUsers = [];

models.Message.find({}, function(err, data) {
    for(var i=0; i < data.length; i++) {
        allUsers.push(data[i].username);
    }
});

console.log(allUsers)

I'm sitting here struggling even getting the allUsers out of the function, and I am thinking this is not even the best way to do this. With allUsers I was just going to check to see if the new username existed in the array.

So to futher extend what I am doing here is some socket.io code. I was going to run some validation like this if I could get the allUsers to work.

socket.on('new user', function (data, callback) {

    if(data in allUsers) {
        callback(false);
    } else {
        callback(true);
        socket.userName = data;
        socket.connected = true;
        users[socket.userName] = socket;
        io.sockets.emit('user name', {usernames: users[socket.userName].userName, connected: users[socket.userName].connected});
    }

});

But without it working, this is no good. So my question is with what I have provided (socket.io, mongodb) how do I get all the models and validate if a new user which is passed in data exists in the database?

1 Answer 1

1

models.Message.find is async, the result of the async operation is only available when the async operation has finished.so console.log(allUsers) will always yield an empty array.

should be something like (pseudo js code):

socket.on('new user', function (data, callback) {

    models.User.findOne({username:data.username},function(err,user){
        if(err){/*deal with error here */}
        else if(user){/*username already taken 
            respond with appropriate socket message here */
                socket.emit('user name already taken',{somemessage});
        }
        else{/* user with username not found */
            /*create new user into database then emit socket message */
            var user = new models.User(data);
            user.save(function(err,user){
                socket.emit('user name',{somemessage});
            })
        }
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect, I had to make some adjustments and do some stuff on the client side.. my testing process is slow, I keep manually restarting node server/ keep running gulp browserify.. I need to work on a faster dev process... I noticed you changed some of my Message names to User you are right it should be that I was going to change it but initially I built the messaging portion then added in users, so some naming is off... thanks though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.