0
我需要從數據庫填充值的數組,然後才返回,所以我怎麼能做出這種同步同步DB請求節點JS ORM
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);
node.js不能像那樣工作。如果你想要看起來同步的東西,可以使用生成器,一種編譯爲異步JavaScript的語言或類似的東西。節點的一大賣點之一是I/O是異步的。 – mscdex