Mongodb 当搜索与文档不匹配时,Mongoose Library find/findOne方法不返回值
我面临着猫鼬的一个问题。当我使用find或findOne方法并且没有匹配结果时,回调函数不会返回null/err并挂起进程。使用Mongoss 5.1.5、MongoDB V3.4.2。请告知Mongodb 当搜索与文档不匹配时,Mongoose Library find/findOne方法不返回值,mongodb,express,mongoose,Mongodb,Express,Mongoose,我面临着猫鼬的一个问题。当我使用find或findOne方法并且没有匹配结果时,回调函数不会返回null/err并挂起进程。使用Mongoss 5.1.5、MongoDB V3.4.2。请告知 module.exports.validateappsubscripition = function (userid, appkey, cb) { //console.error(userid + ' ' + appkey) var userobj_id = mongoose.Types.Objec
module.exports.validateappsubscripition = function (userid, appkey, cb) {
//console.error(userid + ' ' + appkey)
var userobj_id = mongoose.Types.ObjectId(userid);
appsubscripitions.model.findOne({'subscribersuserid': userobj_id , 'appkey'
:appkey }, function(err,doc){
console.error('test2');
if(doc ){
cb(null, doc );
}
else{
cb(null, null );
}
} );
}
调用块:尝试从req头验证密钥。我试图从下面的块调用函数validateAppSubscription
module.exports.sendEmail = function (req, res, next) {
let appkey;
let userid;
if (req.headers.appkey) {
appkey = req.headers.appkey;
console.error(appkey);
}
else {
appkey = '';
}
if(req.user._id){
userid = req.user._id ;
console.error(userid);
}
if (!req.body.fromEmail || !req.body.toEmail || !req.body.subject || !req.body.content) {
res.json({ success: false, msg: 'Please pass all the required parameters' });
next();
}
appsubcripitions.validateappsubscripition(userid, appkey, function (err, doc) {
console.error('test2');
if (err) {
res.json({ success: false, msg: 'Unauthorized. App Key is misssing on the header or App key is not valid' });
next();
}
else if (doc ) {
this.getSMTP('smtp.gmail.com', 'arbalu@gmail.com', function (err, userInfo) {
if (err) {
res.json({ success: false, msg: err.message });
next();
}
if (userInfo) {
//userInfo = user;
var send = require('gmail-send')({
user: userInfo.user,
pass: userInfo.pass,
from: req.body.fromEmail,
to: req.body.toEmail,
subject: req.body.subject, // Override value set as default
text: req.body.content
});
send({ // Overriding default parameters
// to: req.toEmail,
// subject: req.subject, // Override value set as default
// text: req.content
// files: [filepath],
}, function (err, response) {
//console.log('* [example 1.1] send() callback returned: err:', err, '; res:', res);
if (err) {
res.json({ success: false, msg: err.message });
next();
}
else {
res.json({ success: true, msg: response });
next();
}
});
}
})
}
else {
res.json({ success: false, msg: 'Some issue on sending email.Please contact the support.' });
next();
}
});
}请同时添加您的路线代码。此代码似乎没有任何可能导致挂起的问题。添加了调用块以供参考。请看您是否可以帮助我。当您使用
res.json()
时,您不应该调用next
,因为res.json
结束了请求/响应周期。请尝试删除前面有res.json()
调用的next
调用。另外,“test2”是否登录到控制台?我已经对下一个进行了注释,并验证了相同的结果。不变