【问题标题】:How do I modify my returned data from a Mongoose query? [duplicate]如何修改从 Mongoose 查询返回的数据? [复制]
【发布时间】:2015-11-14 23:49:12
【问题描述】:

我已经调用了 lean() 以确保我已将返回的数据转换为普通对象,但只有 stories[x].authorId 没有作为属性添加到当前的 stories[x] 对象中。

有人可以解释原因并提供解决方案吗?它与范围有关还是我无法在另一个查询中修改对象?

Story.find({}).sort({created: -1}).lean().exec(function(err, stories) {
    if (err) throw err;
    for(var x=0; x < stories.length; x++) {
        stories[x].commentsLength = stories[x].comments.length; 
        stories[x].created = stories[x]._id.getTimestamp();
        stories[x].timeAgo = timeHandler(stories[x]);
        User.find({'username': stories[x].author}).lean().exec(function(x, err, data) {
            stories[x].authorId = data[0]._id;
                console.log(stories[x]); // authorId is there
        }.bind(this, x));
        console.log(stories[x]); // authorId is not there
    }
    res.render('news', {message: "Homepage", data: stories});
})

【问题讨论】:

    标签: javascript node.js mongodb mongoose nosql


    【解决方案1】:

    在我看来,您在调用异步 User.find() 函数后尝试注销 stories[x]。因为User.find() 异步运行,所以在调用完成之前您将无法访问其结果。换句话说,这是预期行为。

    还有一点需要注意的是,如果您使用 .forEach 甚至来自 模块的 async.each 之类的迭代器函数,您可能会发现您的代码更易于阅读:

    Story.find({}).sort({created: -1}).lean().exec(function(err, stories) {
        if (err) throw err;
        stories.forEach(function(story) {
            story.commentsLength = story.comments.length;
            story.created = story._id.getTimestamp();
            story.timeAgo = timeHandler(story);
            User.find({
                'username': story.author
            }).lean().exec(function(x, err, data) {
                story.authorId = data[0]._id;
                console.log(story); // authorId is there, because this means User.find completed
            }.bind(this, x));
            console.log(story); // authorId is not there, and never will be, because this function executes before User.find completes its lookup
        });
        res.render('news', {
            message: "Homepage",
            data: stories
        });
    });
    

    如果您尝试修改返回数据,您可以使用 async 来确保在执行回调之前完成所有查找功能:

    function getStory(callback) {
        Story.find({}).sort({created: -1}).lean().exec(function(err, stories) {
            if (err) throw err;
            async.each(stories, function(story, callback) {
                story.commentsLength = story.comments.length;
                story.created = story._id.getTimestamp();
                story.timeAgo = timeHandler(story);
                User.find({
                    'username': story.author
                }).lean().exec(function(x, err, data) {
                    story.authorId = data[0]._id;
                    callback(err, story);
                }.bind(this, x));
            }, function(err, results) {
                callback(err, results); // results is now an array of modified stories
            });
            res.render('news', {
                message: "Homepage",
                data: stories
            });
        })
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-08
      • 2013-11-22
      • 2017-05-16
      • 2020-05-20
      • 1970-01-01
      • 2015-12-07
      相关资源
      最近更新 更多