我想使用findOneAndUpdate()方法来更新用户在html更新表单上输入的数据更新现有模型(Account)。 因此,如果用户只决定更新电话号码字段,则只更新电话号码字段,剩下的两个字段保持不变。Mongodb/Mongoose:如何在快速路径上正确实现findOneAndUpdate
账户模式:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var accountSchema = new Schema({
// Reference to the user model in session.
user: {type: Schema.Types.ObjectId, ref: 'User'},
// User's account information displayed on user's home page
first_name : {type: String},
last_name : {type: String},
phone_number: {type: String}
},
{timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }}
);
module.exports = mongoose.model('Account', accountSchema);
这里是我的路线代码:
app.get('/support', isLoggedIn, function (req, res, next) {
var account = Account({user: req.user});
Account.findOne({user: req.user}, function(err, account) {
if (err) {
res.send(500);
return;
}
console.log(account.first_name)
res.render('support', {user: req.user, account: account});
});
});
app.post('/support', isLoggedIn, function(req, res, next) {
var id = req.params.account._id;
Account.findByIdAndUpdate(id, function(err, doc) {
if (err) {
console.error('error, no entry found');
}
doc.first_name = req.body.first_name || doc.first_name;
doc.last_name = req.body.last_name || doc.last_name;
doc.phone_number = req.body.phone_number || doc.phone_number;
doc.save();
})
res.redirect('/home');
});
GET请求工作正常。我可以访问获取请求上的帐户模型,以向用户显示用户详细信息,但更新路由没有任何操作。我知道我在更新帖子路线设置中丢失了一些东西。 在此先感谢。
嗨大卫,谢谢为了迅速回复......我只有一个问题。我需要在第二个例子(使用promise)之前安装Promise吗? – omosofe
是的,您必须使用外部承诺库,因为猫鼬的承诺库已被弃用。我会更新我的答案,包括承诺:) –
非常感谢大卫,经过与您的代码审查调整后,我能够让我的更新路线工作。我将在稍后发布我的解决方案。我迫不及待想要完成。 再次非常感谢。 – omosofe