NodeJs在函数内部初始化数组

阿南布克

我决定在nodejs项目中使用把手,因此对于索引页面,我想收集与帖子,页面,类别等有关的所有信息。

我有一个函数可以从数据库中返回帖子,如下所示;

exports.getPosts = function(req, res){

    Posts.find({}, function(err, posts) {
        var postsMap = {};

        if (err){
            res.status(400);
        }
        else{

            posts.forEach(function(post) {
              postsMap[post._id] = post;
            });

            res.jsonp(postsMap);
        }
    });
};

我想将该功能更改为以下原型;

function getPosts(req, res){
        var posts = [
                {
                    "url": "#",
                    "title": "home!",
                    "content": "home desc"
                },
                {
                    "url":"#2",
                    "title": "about",
                    "content": "about desc)" 
                }
            ]

    return posts;                    
}

我已经尝试过类似下面的代码,但是posts数组没有初始化,并且返回undefined;

function getPosts(req, res){
    var posts = [];
    Posts.find({}, function(err, posts) {
        var postsMap = {};
        if (err){
            res.status(400);
        }
        else{
            posts.forEach(function(post) {
              postsMap[post._id] = post;
            });
           posts.push(postsMap);
        }
    });
    return posts;
}

我该如何处理这个问题?

丹尼斯·施塔特诺夫(Dennis Shtatnov)

在您的最后一小段代码中,传递给的函数要Posts.find等到函数返回后才能运行

执行顺序为(请参阅注释):

function getPosts(req, res){
    var posts = []; //// 1
    Posts.find({}, function(err, posts) {
        var postsMap = {}; //// 3
        if (err){
            res.status(400);
        }
        else{
            posts.forEach(function(post) {
              postsMap[post._id] = post;
            });
           posts.push(postsMap);
        }
    });
    return posts; // 2
}

这是因为Javascript是异步的,不会等待Post.find完成对数据库的调用。相反,它将继续进行,稍后再调用function(err, posts)

通常要解决此问题,我们会为您的函数提供回调。您的代码可以重构为:

function getPosts(callback){ // Note that i removed res, req from this as it is good practice to separate out request handling from data fetching. Instead I moved it to the usage function mentioned later
    Posts.find({}, function(err, posts) {
        var postsMap = {};
        if (err){
            callback(err);
        }
        else{
            posts.forEach(function(post) {
              postsMap[post._id] = post;
            });
            callback(null, postsMap);
        }
    });
}

使用时getPosts,您可以执行以下操作:

function otherFunction(req, res){
    getPosts(function(err, postsMap){
         // This will start running once getPosts is done

         if(err)
             res.status(400);
         else
            res.jsonp(postsMap);
    })

    // This runs almost immediately and before getPosts is done
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章