我正在尝试更改messages数组以在每个对象中添加userId。
router.post('/messages', (req, res) => {
var users = req.body.users;
users.sort();
Room.findOne({ users: users }, (err, room) => {
if (!room) {
res.status(400).send(errorResponse(null, "Message Not Found."));
} else {
Message.find({ roomId: room._id }, (err, messages) => {
if (messages.length > 0) {
var alteredMessages = [];
for (var i = 0; i < messages.length; i++) {
var userId = messages[i].userId;
var body = messages[i].body;
var createdAt = messages[i].createdAt;
var updatedAt = messages[i].updatedAt;
console.log("1. Calling findUserById");
findUsersById(userId, myCallBack);
function findUsersById(id, callback) {
console.log("2. Inside findUserById");
User.findById(id, (err, user) => {
if (!user) {
console.log("user not found");
} else {
console.log("3. Got User");
callback(user);
}
});
}
function myCallBack(user){
console.log("4. Inside myCallBack. Pushing data to array: " + user.name);
alteredMessages.push(
{
userId,
userName: user.name,
body,
createdAt,
updatedAt
}
);
}
}
setTimeout(function () {
var messageResponse = {
messages: alteredMessages,
users: users
};
console.log("5. Sending response");
console.log(messageResponse);
res.send(successResponse(messageResponse, "Messages Successfully Found."));
}, 10);
}
});
}
});
});
这是终端中控制台日志的结果。 changedMessages数组是响应消息,但所有数据都是原始消息数组的最后索引数据。我正在使用回调,但我不知道它有什么问题。
1. Calling findUserById
2. Inside findUserById
1. Calling findUserById
2. Inside findUserById
1. Calling findUserById
2. Inside findUserById
1. Calling findUserById
2. Inside findUserById
1. Calling findUserById
2. Inside findUserById
1. Calling findUserById
2. Inside findUserById
1. Calling findUserById
2. Inside findUserById
3. Got User
4. Inside myCallBack. Pushing data to array: Manjul Sigdel 2
3. Got User
4. Inside myCallBack. Pushing data to array: Manjul Sigdel 2
3. Got User
4. Inside myCallBack. Pushing data to array: Manjul Sigdel
3. Got User
4. Inside myCallBack. Pushing data to array: Manjul Sigdel
3. Got User
4. Inside myCallBack. Pushing data to array: Manjul Sigdel
3. Got User
4. Inside myCallBack. Pushing data to array: Manjul Sigdel 2
3. Got User
4. Inside myCallBack. Pushing data to array: Manjul Sigdel 2
5. Sending response
{ messages:
[ { userId: '5a49c4417335cd272819d2d1',
userName: 'Manjul Sigdel 2',
body: 'hi bro',
createdAt: 2018-01-12T10:22:05.911Z,
updatedAt: 2018-01-12T10:22:05.911Z },
{ userId: '5a49c4417335cd272819d2d1',
userName: 'Manjul Sigdel 2',
body: 'hi bro',
createdAt: 2018-01-12T10:22:05.911Z,
updatedAt: 2018-01-12T10:22:05.911Z },
{ userId: '5a49c4417335cd272819d2d1',
userName: 'Manjul Sigdel',
body: 'hi bro',
createdAt: 2018-01-12T10:22:05.911Z,
updatedAt: 2018-01-12T10:22:05.911Z },
{ userId: '5a49c4417335cd272819d2d1',
userName: 'Manjul Sigdel',
body: 'hi bro',
createdAt: 2018-01-12T10:22:05.911Z,
updatedAt: 2018-01-12T10:22:05.911Z },
{ userId: '5a49c4417335cd272819d2d1',
userName: 'Manjul Sigdel',
body: 'hi bro',
createdAt: 2018-01-12T10:22:05.911Z,
updatedAt: 2018-01-12T10:22:05.911Z },
{ userId: '5a49c4417335cd272819d2d1',
userName: 'Manjul Sigdel 2',
body: 'hi bro',
createdAt: 2018-01-12T10:22:05.911Z,
updatedAt: 2018-01-12T10:22:05.911Z },
{ userId: '5a49c4417335cd272819d2d1',
userName: 'Manjul Sigdel 2',
body: 'hi bro',
createdAt: 2018-01-12T10:22:05.911Z,
updatedAt: 2018-01-12T10:22:05.911Z } ],
users: [ '5a39fc56e07695104059736f', '5a49c4417335cd272819d2d1' ] }
看到响应,看起来findUserById函数在User.findById()响应用户之前从函数返回。我该如何解决这个问题?
答案 0 :(得分:1)
代码中的问题是您使用setTimeout发送响应。您应该在回调函数myCallBack
中发送响应。像这样。
function myCallBack(user){
console.log("4. Inside myCallBack. Pushing data to array: " + user.name);
alteredMessages.push( { userId, userName: user.name, body, createdAt, updatedAt } );
var messageResponse = { messages: alteredMessages, users: users };
console.log("5. Sending response");
console.log(messageResponse);
res.send(successResponse(messageResponse, "Messages Successfully Found.")); }
}
答案 1 :(得分:-1)
你已经在循环中声明了变量,并且在循环中java脚本不等待下面的函数响应,所以你必须在函数之前将数据推到上面并在下面的代码片段中传递更改用户名的索引我解决了你的问题。按照
var alteredMessages = [];
for (var i = 0; i < 4; i++) {
var userId = i;
var body = "Hello" + i;
var createdAt = "crea" + i;
alteredMessages.push({
userId,
userName: "",
body,
createdAt
});
console.log("1. Calling findUserById");
getUserId(i, i, myCallBack);
}
function getUserId(i, index, callback) {
console.log("2. Inside findUserById");
setTimeout(function() {
if (index == 2) {
callback("Devraj" + i, index);
}
}, 1000)
}
function myCallBack(username, index) {
console.log("4. Inside myCallBack. Pushing data to array: ");
alteredMessages[index].userName = username;
}
setTimeout(function() {
console.log(alteredMessages);
}, 2000);