我是一個新手,我正在用 nodeJS Express mongoDB 做一個后端。
所以我有這個模型:
const user = new Schema({
email: String,
password: String,
lastName: String,
firstName: String
})
module.exports = model('User', user);
然后當用戶注冊時我保存資料:
const createUser = new User({
email: req.body.email,
password: bcrypt.hashSync(req.body.password, 8),
id: req.body.id,
lastName: req.body.lastName,
firstName: req.body.firstName,
photoUrl: req.body.photoUrl,
});
createUser.save((err, user) => {
if (err) {
res.status(500).send({message: err});
}else{
res.send({message: 'Complete'});
}
}
所以我不知道當我添加主模型中不存在的新資料“photoUrl”時是否會影回應用程式或其他 CRUD 功能
uj5u.com熱心網友回復:
Mongoose 默認strict: true在模式上有標志,這基本上意味著傳遞給模型建構式但未在模式中指定的值不會保存到資料庫中。所以基本上所有傳遞的額外欄位都將被跳過。
您必須明確禁用strict才能添加資料庫中未指定的欄位。
以下示例取自貓鼬檔案
// set to false..
const thingSchema = new Schema({..}, { strict: false });
const thing = new Thing({ iAmNotInTheSchema: true });
thing.save(); // iAmNotInTheSchema is now saved to the db!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/443295.html
