简体   繁体   English

猫鼬,推一个数组Model.update不是函数

[英]Mongoose, push an array Model.update is not a function

mongoose categorySchema: 猫鼬categorySchema:

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

My mongoose OthersShema for push array: 我的猫鼬其他希玛推阵列:

    const CategoryDynamiqueSchema = new mongoose.Schema({
        dayOfCategory: { type: Date, default: Date.now },
        nameOfCategory: { type: String },
        typeOfCategory: { type: String }
    });

My API: 我的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 });
        }
    });
});

When I try to push an array I get the following error: 当我尝试推送数组时,出现以下错误:

TypeError: CategoryAdvert.update is not a function TypeError:CategoryAdvert.update不是函数

i have make light change and it's working 我已经改变了灯,它正在工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM