简体   繁体   中英

How to wait for the return of a Mongoose search?

I created a CRUD with NodeJS / Mongoose, split the files in MVC style. In route example I show up below, when the retrieveOne routine is executed, it's necessary to wait for its processing and then redirect the user to one route or another. I'd like to use Bluebird to wait for processing. I need some help to implement the routine.

Index.js -----------------------------------------------

const myCRUD = require('./api/controllers/controller')

router.post('/login', function(req, res, next) {   

   // HOW TO IMPLEMENT BLUEBIRD HERE?

   // How to wait for the "retrieveOne" process and a then do a "if" test (below)?   
   let ret = myCRUD.retrieveOne({ name: "abc test" }); 


   if(!ret) {
      res.redirect('/success')
   } else {
      res.redirect('/error')
   }
})

controller.js ------------------------------------------

const mongoose = require('mongoose');
const Schema = require('./schema-user');
const Model = mongoose.model('users', Schema);

const CRUD = {
   retrieveOne: function(query) {
      Model.findOne(query, function(err, result) {
         if (err) return err;
         return result;
      });
   }
}

module.exports = CRUD;

Note : I've already found several examples with and without Bluebird right here in SO, but I couldn't get it to work: Examples: 1 , 2 , 3 , 4 , 5

You don't really need to use Bluebird for this, as mongoose works just fine with native Promises

In your controller:

const mongoose = require('mongoose');
const Schema = require('./schema-user');
const Model = mongoose.model('users', Schema);

const CRUD = {
   retrieveOne: function(query) {
      // findOne() returns a Promise ()
      // https://mongoosejs.com/docs/promises.html
      return Model.findOne(query);
   }
}

module.exports = CRUD;

Now, you can make your route handler an async function and just await the Promise

const myCRUD = require('./api/controllers/controller')

router.post('/login', async function(req, res, next) { 
   let ret = await myCRUD.retrieveOne({ name: "abc test"}); 
   if(!ret) {
      res.redirect('/success')
   } else {
      res.redirect('/error')
   }
})

Using async and await

const myCRUD = require('./api/controllers/controller')
const Promise = require('bluebird');

router.post('/login', async function(req, res, next) {   

   // HOW TO IMPLEMENT BLUEBIRD HERE?

   // How to wait for the "retrieveOne" process and a then do a "if" test (below)?   
   let ret = await myCRUD.retrieveOne('{ name: "abc test"'); 


   if(!ret) {
      res.redirect('/success')
   } else {
      res.redirect('/error')
   }
})

// controller.js ------------------------------------------

const mongoose = require('mongoose');
const Schema = require('./schema-user');
const Model = mongoose.model('users', Schema);

const CRUD = {
   retrieveOne: function(query) {
      return new Promise((resolve, reject) => {
        Model.findOne(query, function(err, result) {
           if (err) reject(err);
           resolve(result);
        });
      });
   }
}

module.exports = CRUD;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM