Return results mongoose in find query to a variable

前端 未结 4 609
再見小時候
再見小時候 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条回答
  • 2020-12-12 16:18

    It is being executed before the assignment.

     async function(req, res) {
          var user;
          await users.findOne({}, function(err,pro){
              user=pro;
            });
          console.log(user); \\ it's define
        };
    
    0 讨论(0)
  • 2020-12-12 16:18

    You can easily achieve this.

        const getUser = async ( req, res ) => {
            let users = () => ( User.find({_id : users_list[i]['user_id']},{email : 1, credits : 1}).exec() );
            try {  res.send({"user":await users() });}
            catch(e) { console.log(e) }
        }    
        app.get('/user' , getUser);
    
    0 讨论(0)
  • 2020-12-12 16:21

    There are several ways to achieve what you want.

    1. Using Mongoose Queries

    In this strategy, your function returns a Mongoose query which you can later use to invoke the method exec and use it to get the results.

    function getJedisQuery(name){
       var query = Jedi.find({name:name});
       return query;
    }
    

    Then you can use it simply doing:

    var query =  getJedisQuery('Obi-wan');
    query.exec(function(err,jedis){
       if(err)
          return console.log(err);
       jedis.forEach(function(jedi){
          console.log(jedi.name);
       });
    });
    

    2. Using Mongoose Promise-like Objects

    Moogose provides support for promise-like objects. All you have to do is something somewhat similar to what I did above, but this time, you invoke the exec method without a callback.

    function getJedisPromise(name){
       var promise = Jedi.find({name:name}).exec();
       return promise;
    }
    

    Then you can use it by simply doing:

    var promise = getJedisPromise('Luke');
    promise.then(function(jedis){
       jedis.forEach(function(jedi){
          console.log(jedi.name);
       });
    })
    

    As highlighted in the comment section of this answer, these objects are not in fact promises and that needs to be taken into account (see Queries are not promises).

    3. Using Mongoose Streams

    Finally, Mongoose has also support for streams and streams are event emitters. So, you could get a stream and then subscribe for 'data' and 'error' events. Like this:

    function getjedisStream(name){
       var stream = Jedi.find({name:name}).stream();
       return stream;
    }
    

    Then you can simply do:

    var stream = getJedisStream('Anakin');
    stream.on('data', function(jedis){
       jedis.forEach(function(jedi){
          console.log(jedi.name);
       });
    });
    stream.on('error', function(error){
        console.log(error);
    });
    

    Source, for future reference.

    0 讨论(0)
  • 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);
    });
    
    0 讨论(0)
提交回复
热议问题