使用mongodb调用的Node.js express无法完成执行



我有一个.post函数,在这里我使用mongodb调用,类似于以下内容:

router.post('/insert')=>{
-findOne
// Some code
// with error return redirect('/error')
-Count
// with error return redirect('/error')
-Insert
// with error return redirect('/error') 
// with normal execution return redirect('/save')
}

我的问题是,在这个重播之后,这个函数的执行没有完成。所以,如果findOne失败后我检查的内容,我不想进入计数或插入

注意,我已经在使用return redirect()

编辑添加一些代码:

router.post('/insert_article', common.restrict, (req, res) => {
db.findOne({
_id: root_id
},(err, item) => {
if (item.language !== req.body.form_language) {
req.session.message = req.i18n.__('Language does not match');
req.session.message_type = 'Fail!';
return res.redirect(req.app_context + '/insert');
}
})
db.insert(doc, (err, newDoc) => {
if (err) {
console.error('Error inserting document: ' + err);
} else {
//some stuff
req.session.message = req.i18n.__('New article successfully created');
req.session.message_type = 'success';
// redirect to new doc
res.redirect(req.app_context + '/edit/' + newId);
}
})
}

我想现在更清楚了,当"语言"不匹配时,我无论如何都不想做这个插入,而且一直这样做

编辑:使用更新的代码,很明显您正试图从回调返回,请参阅下面的片段

如果没有更多的代码,很难说,但我的最佳猜测是,您在回调函数内部使用return redirect('/error')(例如,如果您对数据库方法使用promise,则为.then提供的回调(,因此,您不是从整个函数返回,而是从回调返回,然后继续通过路由处理程序。如果是这种情况,那么最好使用异步路由处理程序,这样您就可以await数据库调用,如果出现问题,则可以return res.redirect,这将如您所期望的那样起作用

您的代码在从回调返回的地方执行此操作,因此路由处理程序内部的执行将继续

router.post('/insert_article', common.restrict, (req, res) => {
db.kb.findOne({
_id: root_id
},(err, item) => {
if (item.language !== req.body.form_language) {
req.session.message = req.i18n.__('Language does not match');
req.session.message_type = 'Fail!';
// the following line will return from your callback, NOT the route handler
return res.redirect(req.app_context + '/insert');
}
})
// because you returned from the callback, code here will continue to execute
});

你可以做这个

router.post('/insert_article', common.restrict, (req, res) => {
db.kb.findOne({
_id: root_id
},(err, item) => {
if (item.language !== req.body.form_language) {
req.session.message = req.i18n.__('Language does not match');
req.session.message_type = 'Fail!';
return res.redirect(req.app_context + '/insert'); // returns from callback, not the route handler
}
db.kb.insert(doc, (err, newDoc) => {
if (err) {
console.error('Error inserting document: ' + err);
} else {
//some stuff
req.session.message = req.i18n.__('New article successfully created');
req.session.message_type = 'success';
// redirect to new doc
res.redirect(req.app_context + '/edit/' + newId);
}
});
}
})
});

但上述方法将创建"回调地狱",即回调中的回调。。。等等

更好的方法是使用promise而不是回调函数和async/await语法:

router.post('/insert_article', common.restrict, async (req, res) => {
try {
const item = await db.kb.findOne({ _id: root_id });
if (item.language !== req.body.form_language) {
req.session.message = req.i18n.__('Language does not match');
req.session.message_type = 'Fail!';
return res.redirect(req.app_context + '/insert');
}
await db.kb.insert(doc); // if there is an error, it will be caught in the catch block
req.session.message = req.i18n.__('New article successfully created');
req.session.message_type = 'success';
// redirect to new doc
return res.redirect(req.app_context + '/edit/' + newId);
} catch (e) {
return res.redirect(req.app_context + '/insert');
}
});

最新更新