最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to do a findOneAndUpdate with bluebird promises (mongoose) - Stack Overflow

matteradmin3PV0评论

I cannot find any examples on how to resolve promises with bluebird when using mongoose's findOneAndUpdate.

var Promise = require("bluebird");
var mongoose = Promise.promisifyAll(require('mongoose'));

//actions is an array of objects with queries and upsert data

var promises = actions.map(function(arr) {
    return MyModel.findOneAndUpdate(arr.query, arr.upsertData, {'upsert': true}, function (err, doc) {
        if (err) {
            console.log('Error: ', err);
            //return err;
        } else {
            console.log('doc: ', doc);
            //return Promise.resolve();
        }
    });
});
//once all db transactions are finished:
Promise.all(promises)
    .then(function() {

        console.log('all done');

        //this is where I want to output all the documents once they are updated or added

    })
    .error(function(err) {
        console.log('error:', err);
    });

So far, I've looked at bluebird and also async. I need to pass different options to the query, which I could not get to work with either library.

I cannot find any examples on how to resolve promises with bluebird when using mongoose's findOneAndUpdate.

var Promise = require("bluebird");
var mongoose = Promise.promisifyAll(require('mongoose'));

//actions is an array of objects with queries and upsert data

var promises = actions.map(function(arr) {
    return MyModel.findOneAndUpdate(arr.query, arr.upsertData, {'upsert': true}, function (err, doc) {
        if (err) {
            console.log('Error: ', err);
            //return err;
        } else {
            console.log('doc: ', doc);
            //return Promise.resolve();
        }
    });
});
//once all db transactions are finished:
Promise.all(promises)
    .then(function() {

        console.log('all done');

        //this is where I want to output all the documents once they are updated or added

    })
    .error(function(err) {
        console.log('error:', err);
    });

So far, I've looked at bluebird and also async. I need to pass different options to the query, which I could not get to work with either library.

Share Improve this question asked Jun 18, 2015 at 12:54 reggiereggie 3,67414 gold badges64 silver badges102 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

Mongoose's Query class, an instance of which findOneAndUpdate() returns, has an .exec() method that returns a promise:

var promises = actions.map(function(arr) {
  return MyModel.findOneAndUpdate(arr.query, arr.upsertData, {'upsert': true}).exec();
});

You get the results in an array:

Promise.all(promises).then(function(results) { ... });
Post a comment

comment list (0)

  1. No comments so far