【发布时间】:2017-01-28 20:22:36
【问题描述】:
我正在尝试从帖子中获取所有内容和媒体,然后附加到新帖子以发送到响应以便轻松访问数据。问题是,每当我运行调用时,都会在获取内容和媒体之前发送响应。
module.exports.getPosts = function(req,res){
var resultPosts = [];
Post.findAll({where:{UserUsername:req.headers.x_username}}).then(function(posts){
posts.forEach(function(post){
Db.getContent(post.id,function(foundContent){
Db.getMedia(post.id,function(foundMedia){
console.log('creating new post');
var newPost = {
id:post.id,
time:post.time,
username:post.UserUsername,
content: foundContent,
media:foundMedia
};
resultPosts.push(newPost);
});
});
});//foreach
console.log('sending');
res.send({posts:resultPosts});
});//end query
};
【问题讨论】:
-
这是不正确的。它很接近,但会失败。
标签: javascript node.js express foreach sequelize.js