【发布时间】:2016-06-24 15:42:43
【问题描述】:
所以我有一个帖子集合
{
id: String,
comments: [String], # id of Comments
links: [String], #id of Links
}
评论: { id:字符串, 评论:字符串, }
链接: { id:字符串, 链接:字符串, }
用 id 找到一个带有 cmets 和链接的帖子:
Posts.findOne({id: id}, function(post) {
Comments.find({id: post.id}, function(comments) {
Links.find({id: post.id}, function(links) {
res.json({post: post, comments: comment, links: links})
})
})
})
如何使用 Promise(http://mongoosejs.com/docs/promises.html) 避免回调地狱?
var query = Posts.findOne({id: id});
var promise = query.exec();
promise.then(function (post) {
var query1 = Comments.find({id: post.id});
var promise1 = query1.exec();
promise1.then(function(comments) {
var query2 = Links.find({id: post.id});
var promise2 = query2.exec();
promise2.then(function(links) {
res.json({post: post, comments: comment, links: links})
})
})
});
好像不好......
【问题讨论】:
标签: javascript node.js mongoose promise