【问题标题】:Sequelize Foreach loop sends response before updating dataSequelize Foreach 循环在更新数据之前发送响应
【发布时间】: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


【解决方案1】:

请记住您使用异步代码,正如您所说,您在 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  

【讨论】:

    【解决方案2】:

    您必须使整个函数异步。在这种特殊情况下,您必须首先遍历所有结果,然后输出到 res。你可以使用 async.forEach() 方法来解决这个问题:https://codeburst.io/javascript-async-await-with-foreach-b6ba62bbf404

    我会用迭代器做一个简单的异步递归函数,让它保持原生而不需要额外的库。

    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)
                   }
               })
             })
    

    【讨论】:

      猜你喜欢
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 2019-05-19
      相关资源
      最近更新 更多