我需要用数据库中的值填充数组,然后才返回它,所以我怎样才能使这个同步
var convs = Array;
User.find({token: token}).first(function(err, user) {
ConversationData.find({user_id: 1}).each().forEach(function(convData) {
ConversationData.count({conversation_id: convData.conversation_id}, function(err, count) {
if(count == 2) {
var user2;
console.log(convData.user_id);
ConversationData.find({user_id: 2, conversation_id: convData.conversation_id}).first(function(err, usr) {
user2 = usr;
});
Message.find({conversation: convData.conversation_id}, [ "createdAt", "Z" ]).first(function(err, msg){
convs[convData.id].conversation = convData.id;
convs[convData.id].lastMessage = msg.content;
convs[convData.id].lastMessageDate = msg.createdAt;
convs[convData.id].title = user2.name + " " + user2.name;
convs[convData.id].avatar = user2.avatar;
});
} else {
console.log('COUNT = ' + count);
}
});
});
console.log(convs);