How to wait for the result of a mongoose query?

后端 未结 1 380
名媛妹妹
名媛妹妹 2021-01-28 07:37

I try to filter an array according to the result of a mongoose query. The standard filter function expect the callback to return true or false. My trouble is that this informati

相关标签:
1条回答
  • 2021-01-28 08:27

    Use the filter function from the async library instead.

    UPDATE

    You're pretty close in your attempt, you just need to provide the result to the callback instead of returning it:

    async.filter(myArray, function(elem, callback){
      MyCollection.findOne({_id : elem}, function(err, doc) {
        callback(err == null && doc != null);
      });
    }, 
    function(results){
      // results is myArray filtered to just the elements where findOne found a doc
      // with a matching _id.
    });
    
    0 讨论(0)
提交回复
热议问题