【问题标题】:Mongoose, push an array Model.update is not a functionMongoose,推送一个数组 Model.update 不是函数
【发布时间】:2018-10-11 20:44:33
【问题描述】:

猫鼬类别架构:

    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 不是函数

【问题讨论】:

    标签: javascript arrays node.js mongoose push


    【解决方案1】:

    我做了一些改变,它正在工作

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

    【讨论】:

      猜你喜欢
      • 2018-07-05
      • 1970-01-01
      • 2016-03-25
      • 2016-03-18
      • 2015-05-11
      • 2019-07-18
      • 1970-01-01
      • 2021-09-01
      • 2018-09-14
      相关资源
      最近更新 更多