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; 注意:我已经在S.O.中找到了一些带有和不带有Bluebird的示例,但是我无法使它正常工作:示例: 1 , 2 , 3 , 4 , 5Note: 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推荐答案使用 async 和 awaitconst 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; 这篇关于如何等待猫鼬搜索的返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 18:07