我如何更改属性' avatar'只有定义了吗?
update: function(req, res){
Student.findOneAndUpdate({ _id: req.params.id }, {
first_name : req.body.first_name,
last_name : req.body.last_name,
_school : req.body._school,
_teacher : req.body._teacher,
grade : req.body.grade,
language : req.body.language,
avatar : avatar,
address : req.body.address,
city : req.body.city,
state : req.body.state,
zip : req.body.zip,
_guardians : req.body._guardians,
updated_at : req.body.updated_at,
active : req.body.active },
function(err, student){
if(err) res.render('error', { error: 'Error updating student'});
res.redirect('/admin/students');
});
}
我正在寻找这样的事情: var avatar = req.file? req.file.fieldname:user.avatar;
然后在' findOneAndUpdate'
的更新参数中使用var avatar非常感谢任何帮助!
谢谢!
答案 0 :(得分:1)
我不知道你的应用程序或它的逻辑,所以我不知道你想如何解析和定义头像,所以这取决于你(它可以类似于你发布的内容)。
但除此之外,你可以做这样简单的事情。
update: function(req, res){
var options = {
first_name : req.body.first_name,
last_name : req.body.last_name,
_school : req.body._school,
_teacher : req.body._teacher,
grade : req.body.grade,
language : req.body.language,
address : req.body.address,
city : req.body.city,
state : req.body.state,
zip : req.body.zip,
_guardians : req.body._guardians,
updated_at : req.body.updated_at,
active : req.body.active }
if (avatar) {
options.avatar = avatar;
}
Student.findOneAndUpdate({ _id: req.params.id }, options,
function(err, student){
if(err) res.render('error', { error: 'Error updating student'});
res.redirect('/admin/students');
});
}