当我从POST保存新作者时,我想返回一条消息,表明新作者已被保存。 (如果作者已经存在,我也最终会想要返回一些内容)我遇到的问题是,虽然作者在数据库中保存,但我无法从内部写入res.locals.messages
author.save()的回调参数。 (另请注意,createAuthor是一个调用数组 - 我不确定这是否会使result
的值变得更加困难。)
如何将result
的值传递给路由器?
路线:
router.post('/', authorController.createAuthor, (req, res) => {
res.json({ messages: res.locals.messages})
})
控制器:
exports.createAuthor = [
check('firstName', 'First name must be specified').isLength({min: 1}).trim(),
check('lastName', 'Last name must be specified').isLength({min: 1}).trim(),
sanitizeBody('firstName').trim().escape(),
sanitizeBody('lastName').trim().escape(),
(req, res, next) => {
const errors = validationResult(req);
var author = new Author({
firstName: req.query.firstName,
lastName: req.query.lastName
});
if (errors.isEmpty()) {
//Edit: added find before save function:
const query = {$or:[{firstName:{$regex: req.query.firstName, $options: 'i'}},
{lastName:{$regex: req.query.lastName, $options: 'i'}}]} //TODO this regex fails if a parameter is missing.
Author.findOne(query).sort([['lastName', 'ascending']]).exec((err, author) => {
if (err) {
return next(err)
}
if(!(author ==null)){
var result = {status: "Author already exists: ", author_id: String(author.id)}
res.locals.messages = result;
next();
}
});
author.save(function (err, newAuthor) {
if (err) {
return next(err)
}
var result = {status: "New author saved", author_id: String(newAuthor.id)}
res.locals.messages = result;
});
}
next();
}
];
答案 0 :(得分:1)
您需要在处理保存后调用next
并设置res.locals
值:
exports.createAuthor = [
check('firstName', 'First name must be specified').isLength({min: 1}).trim(),
check('lastName', 'Last name must be specified').isLength({min: 1}).trim(),
sanitizeBody('firstName').trim().escape(),
sanitizeBody('lastName').trim().escape(),
(req, res, next) => {
const errors = validationResult(req);
var author = new Author({
firstName: req.query.firstName,
lastName: req.query.lastName
});
if (errors.isEmpty()) {
author.save(function (err, newAuthor) {
if (err) {
return next(err)
}
var result = {status: "New author saved", author_id: String(newAuthor.id)}
res.locals.messages = result;
next();
});
} else {
// Remember to do something about these non-empty errors!
next(new Error('Got more than 0 errors'));
}
}
];
另外,不要忘记处理验证错误数非零的情况:)
答案 1 :(得分:0)
使用克里斯福斯特的评论,我能够做到这一点:
exports.createAuthor = [
check('firstName', 'First name must be specified').isLength({min: 1}).trim(),
check('lastName', 'Last name must be specified').isLength({min: 1}).trim(),
sanitizeBody('firstName').trim().escape(),
sanitizeBody('lastName').trim().escape(),
(req, res, next) => {
const errors = validationResult(req);
var author = new Author({
firstName: req.query.firstName,
lastName: req.query.lastName
});
if (errors.isEmpty()) {
//TODO - Fix this query.
const query = {
$or: [{firstName: {$regex: req.query.firstName, $options: 'i'}},
{lastName: {$regex: req.query.lastName, $options: 'i'}}]
}
Author.findOne(query).sort([['lastName', 'ascending']]).exec((err, existingAuthor) => {
if (err) {
return next(err)
}
if (!(existingAuthor == null)) {
var result = {status: "Author already exists: ", author_id: String(existingAuthor.id)}
res.locals.messages = result;
next();
} else if (existingAuthor == null) {
author.save(function (err, newAuthor) {
if (err) {
return next(err)
}
var result = {status: "New author saved", author_id: String(newAuthor.id)}
res.locals.messages = result;
next();
});
}
});
}
}
];