javascript - Mongoose,推送一个数组 Model.update 不是函数
问题描述
猫鼬类别架构:
const CategoryAdvertSchema = new mongoose.Schema({
UniqueHomes: {
cave: { type: Boolean, default: false },
natureLodge: { type: Boolean, default: false },
castle: { type: Boolean, default: false },
farmStay: { type: Boolean, default: false }
},
PropertyType: {
apartment: { type: Boolean, default: false },
villa: { type: Boolean, default: false },
loft: { type: Boolean, default: false },
yurt: { type: Boolean, default: false }
},
Others: [CategoryDynamiqueSchema]
});
我的猫鼬 OthersShema 用于推送数组:
const CategoryDynamiqueSchema = new mongoose.Schema({
dayOfCategory: { type: Date, default: Date.now },
nameOfCategory: { type: String },
typeOfCategory: { type: String }
});
我的 API:
category.post('/category', jwt.checkUserToken, (req, res) => {
const dayOfCategory = Date.now();
const nameOfCategory = req.body.nameOfCategory;
const typeOfCategory = req.body.typeOfCategory;
CategoryAdvert.update({
$push: {
Others: {
dayOfCategory: dayOfCategory,
nameOfCategory: nameOfCategory,
typeOfCategory: typeOfCategory
}
}
}, { new: true }, (err, category) => {
if (err) {
res.json({ success: false });
console.log('err : ', err);
} else {
console.log("La catégorie '" + nameOfCategory + "' a bien été ajouté");
res.json({ success: true });
}
});
});
当我尝试推送数组时,出现以下错误:
TypeError:CategoryAdvert.update 不是函数
解决方案
我做了一些改变,它正在工作
category.post('/category', jwt.checkUserToken, (req, res) => {
console.log('req.body => ', req.body);
const dayOfCategory = Date.now();
const nameOfCategory = req.body.nameOfCategory;
const typeOfCategory = req.body.typeOfCategory;
Advert.update({
$push: {
'CategoryAdvert.Others': {
dayOfCategory: dayOfCategory,
nameOfCategory: nameOfCategory,
typeOfCategory: typeOfCategory
}
}
}, { new: true }, (err, category) => {
if (err) {
res.json({ success: false });
console.log('err : ', err);
} else {
console.log("La catégorie '" + nameOfCategory + "' a bien été ajouté");
res.json({ success: true });
}
});
});
推荐阅读
- json - 如何使用`jq`将键复制到对象中?
- c# - c# wpf MenuItems标题的点击事件
- python - pymodbus“单位”参数是什么意思?
- ios - 通过访问 AppDelegate 中的 Tab Bar Items 更改 Tab Bar Item Image inset
- typescript - 类型保护适用于错误类型但接受未知类型
- spring - 在 jhipster 的导航栏中显示用户名
- c# - C# 任务被取消
- r - 将数据框存储到 R 中的 SQLite 数据库时如何转换 lubridate 日期对象?
- javascript - JavaScript:多个参数
- windows-10 - 在 2018 年 5 月 Windows 10 功能更新 1803 之后,邮槽上的 CreateFile 失败并出现错误 53 ERROR_BAD_NETPATH