Underscore _.contains 方法在 mongoose promise 查询中总是失败
Underscore _.contains method always failed inside mongoose promise query
您好,我在 socket.io 事件中使用 mongoose 查询,目前一切顺利。我想检查用户是否是私人房间的成员,然后获取消息。所以我使用下划线方法 _.contains() 但总是 return 错误。我不知道代码有什么问题,因为它记录了列表和选中的项目,它应该 return true
socket.on('add user', function (data) {
var username = data.username;
var userId = data.userId;
var currentroom = data.room ? data.room : '559c02cfd2ad52cc276b7491';
// we store the username in the socket session for this client
socket.username = username;
socket.userid = userId;
// add the client's username to the global list
usernames[username] = username;
++numUsers;
socket.emit('login', {
numUsers: numUsers
});
var room = new roomModel();
return room.getRoomParticipants(currentroom)
.then(function(res) {
console.log('checking room');
console.log(res);
if (!res) {
throw new Error('Couldn\'t get room participants!');
}
res = res[0];
var participants = res.participants;
return participants;
})
.then(function(participants) {
if (!_.contains(participants, socket.userid)) {
console.log('have failed');
return null;
}
_.each(participants, function(item) {
var user = new userModel();
user.getById(item)
.then(function(res) {
if (res) {
rommates[res._id] = res;
}
})
});
return roommates;
})
.then(function(participants) {
var message = new messageModel();
message.roomMessages(currentroom, msgLimit)
.then(function(res) {
_.each(res, function(item) {
var data = {};
var user = rommates[res._id];
data.username = user.username;
data.message = item.msg;
data.time = item.created_at;
socket.emit('new message', data);
});
});
})
.then(null, function(err) {
logger.debug(err);
});
});
通过替换解决了
if (!_.contains(participants, socket.userid)) {
return null;
}
和
var participants = _.object(res.participants);
var allowed = _.find(res.participants, function(item) {
return item == socket.userid;
});
if (!allowed) {
throw new Error('User ' + socket.userid + ' is not allowed to room ' + currentroom);
}
您好,我在 socket.io 事件中使用 mongoose 查询,目前一切顺利。我想检查用户是否是私人房间的成员,然后获取消息。所以我使用下划线方法 _.contains() 但总是 return 错误。我不知道代码有什么问题,因为它记录了列表和选中的项目,它应该 return true
socket.on('add user', function (data) {
var username = data.username;
var userId = data.userId;
var currentroom = data.room ? data.room : '559c02cfd2ad52cc276b7491';
// we store the username in the socket session for this client
socket.username = username;
socket.userid = userId;
// add the client's username to the global list
usernames[username] = username;
++numUsers;
socket.emit('login', {
numUsers: numUsers
});
var room = new roomModel();
return room.getRoomParticipants(currentroom)
.then(function(res) {
console.log('checking room');
console.log(res);
if (!res) {
throw new Error('Couldn\'t get room participants!');
}
res = res[0];
var participants = res.participants;
return participants;
})
.then(function(participants) {
if (!_.contains(participants, socket.userid)) {
console.log('have failed');
return null;
}
_.each(participants, function(item) {
var user = new userModel();
user.getById(item)
.then(function(res) {
if (res) {
rommates[res._id] = res;
}
})
});
return roommates;
})
.then(function(participants) {
var message = new messageModel();
message.roomMessages(currentroom, msgLimit)
.then(function(res) {
_.each(res, function(item) {
var data = {};
var user = rommates[res._id];
data.username = user.username;
data.message = item.msg;
data.time = item.created_at;
socket.emit('new message', data);
});
});
})
.then(null, function(err) {
logger.debug(err);
});
});
通过替换解决了
if (!_.contains(participants, socket.userid)) {
return null;
}
和
var participants = _.object(res.participants);
var allowed = _.find(res.participants, function(item) {
return item == socket.userid;
});
if (!allowed) {
throw new Error('User ' + socket.userid + ' is not allowed to room ' + currentroom);
}