Javascript Sequelize Foreach循环在更新数据之前发送响应

Javascript Sequelize Foreach循环在更新数据之前发送响应,javascript,node.js,express,foreach,sequelize.js,Javascript,Node.js,Express,Foreach,Sequelize.js,我试图从一篇文章中获取所有内容和媒体,然后附加到一篇新的文章中,以发送到响应中,以便轻松访问数据。问题是,每当我运行调用时,都会在获取内容和媒体之前发送响应 module.exports.getPosts = function(req,res){ var resultPosts = []; Post.findAll({where:{UserUsername:req.headers.x_username}}).then(function(posts){ posts.forEach(func

我试图从一篇文章中获取所有内容和媒体,然后附加到一篇新的文章中,以发送到响应中,以便轻松访问数据。问题是,每当我运行调用时,都会在获取内容和媒体之前发送响应

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  

})

记得您使用异步代码,正如您所说,您在getMedia回调完成之前调用res.send。将res.send移动到该回调,以确保在完成推送到resultPosts数组后调用它

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);
                console.log('sending');
                res.send({posts:resultPosts});
            });
        });
    });//foreach

});//end query  

您必须使整个函数异步。在这种特殊情况下,您必须首先对所有结果进行forEach,然后将结果输出到res。您可以使用async.forEach()方法来解决此问题:

我将使用迭代器执行一个简单的异步递归函数,使其保持所有本机状态,而不需要任何额外的库

var resultPosts = []
        Post.findAll({where:{UserUsername:req.headers.x_username}}).then(function(posts) 
        {
                getPost(posts[Symbol.iterator]())
                function getPost(iter) {
                        var cursor = iter.next()
                        if(cursor.done)
                           return res.send({posts:resultPosts})
                  Db.getContent(post.id,function(foundContent){
                     Db.getMedia(post.id,function(foundMedia){
                         var post = cursor.value
                        resultPosts.push({
                          id:post.id,
                          time:post.time,
                           username:post.UserUsername,
                             content: foundContent,
                          media:foundMedia
                         })
                  getPost(iter)
               }
           })
         })

这是不对的。很接近,但会失败。