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.
我正在使用mongodb的套接字,对于试图创建新名称的用户,我需要检查数据库中的所有模型以查看它是否存在。
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.
我坐在这里努力让allUsers脱离这个功能,我认为这甚至不是最好的方法。使用allUsers,我只是要查看数组中是否存在新的用户名。
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.io代码。如果我可以让allUsers工作,我将会运行这样的验证。
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?
但如果没有它的工作,这是不好的。所以我的问题是我提供的内容(socket.io,mongodb)如何获取所有模型并验证数据库中是否存在传入数据的新用户?
1 个解决方案
#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.
models.Message.find是异步的,异步操作的结果仅在异步操作完成时可用.so console.log(allUsers)将始终产生一个空数组。
should be something like (pseudo js code):
应该像(伪js代码):
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});
})
}
});
});
#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.
models.Message.find是异步的,异步操作的结果仅在异步操作完成时可用.so console.log(allUsers)将始终产生一个空数组。
should be something like (pseudo js code):
应该像(伪js代码):
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});
})
}
});
});