gpt4 book ai didi

javascript - Mongoose,推送数组 Model.update 不是函数

转载 作者:太空宇宙 更新时间:2023-11-04 01:49:53 25 4
gpt4 key购买 nike

Mongoose 类别架构:

    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]
});

我的 mongoose 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 is not a function

最佳答案

我已经改变了灯光并且它正在工作

    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 });
}
});
});

关于javascript - Mongoose,推送数组 Model.update 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50122797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com