Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

node.js - Return results mongoose in find query to a variable

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:

var results = users.findOne({_id : users_list[i]['user_id']},{email : 1, credits : 1},{}, function(err, docs) {
    if( err || !docs) {
        console.log("No user found");
    } else {            
        return docs;
    };
});

In order to have:

results = docs 

Thanks a lot for your reply .

I also have another problem.

How to pass variable in a query operator with find or findOne? Like :

var foo = "Blaa";

users.findOne({_id : users_list[i]['user_id']},{email : 1, credits : 1},{}, function(err, docs) {
    if( err || !docs) {
        console.log("No user found");
    } else {
        // I want to use the foo variable here
        console.log(foo);
    };
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...