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 S.O., but I couldn't get it to work: Examples: 1, 2, 3, 4, 5