Return results mongoose in find query to a variable

前端 未结 4 613
再見小時候
再見小時候 2020-12-12 15:39

I need to return the results of a query with mongoose in node.js.

How do you return the value to set the value to a variable?

What I need to do is:

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 16:26

    You achieve the desired result by the following code. Hope this will helps you a lot..

    var async = require('async');
    
    // custom imports
    var User = require('../models/user');
    var Article = require('../models/article');
    
    var List1Objects = User.find({});
    var List2Objects = Article.find({});
    var resourcesStack = {
        usersList: List1Objects.exec.bind(List1Objects),
        articlesList: List2Objects.exec.bind(List2Objects),
    };
    
    async.parallel(resourcesStack, function (error, resultSet){
        if (error) {
            res.status(500).send(error);
            return;
        }
        res.render('home', resultSet);
    });
    

提交回复
热议问题