如何使用Node将对象添加到嵌套数组中。js和猫鼬

时间:2022-04-28 19:39:36

How can I add object to my nested Array in PartnerSchema?

我如何在PartnerSchema中添加对象到我的嵌套数组?

I separate documents, because in the future there will be more of nested arrays.

我分离文档,因为将来会有更多的嵌套数组。

This is my schema:

这是我的模式:

var productSchema = new mongoose.Schema({
    name: String
});

var partnerSchema = new mongoose.Schema({
    name: String,
    products: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Product'
        }]
});

module.exports = {
    Partner: mongoose.model('Partner', partnerSchema),
    Product: mongoose.model('Product', productSchema)
}

And this is my backend:

这是我的后端:

var campSchema = require('../model/camp-schema');

router.post('/addPartner', function (req, res) {
    new campSchema.Partner({ name : req.body.name }).save(function (err, response) {
        if (err) console.log(err);
        res.json(response);
    });
});

router.post('/addProduct', function (req, res) {
    campSchema.Partner.findByIdAndUpdate({ _id: req.body.partnerId }, 
        {
        $push: {
            "products": {
                name: req.body.dataProduct.name
            }
        }
    }, { safe: true }, function (err, response) {
        if (err) throw err;
        res.json(response);
    });
});

I can add Partner by using /addPartner and it works fine.

我可以通过使用/addPartner添加合作伙伴,它工作得很好。

Problem is with second function /addProduct I can't add Product to Array in Partner Schema. I have an error: CastError: Cast to undefinded failed for value "[object Object]" at path "products"

问题是第二个函数/addProduct不能在合作伙伴模式中向数组添加Product。我有一个错误:CastError: Cast to undefinded failed for value "[object object]" at path "products"

1 个解决方案

#1


3  

Since the products field in Partner model is an array that holds _id refs to the Product model, you are supposed to push an _id to the array, not an object hence Mongoose complains with an error.

由于Partner模型中的products字段是一个将_id refs保存到Product模型的数组,所以应该将_id推到数组中,而不是对象,因此Mongoose会报错。

You should restructure your code to allow the saving of the Product _id ref to the Partner model:

您应该重构代码,以便将产品_id ref保存到合作伙伴模型:

router.post('/addProduct', function (req, res) {
    var product = new campSchema.Product(req.body.dataProduct);

    product.save(function (err) {
        if (err) return throw err;        
        campSchema.Partner.findByIdAndUpdate(
            req.body.partnerId,
            { "$push": { "products": product._id } },
            { "new": true },
            function (err, partner) {
                if (err) throw err;
                res.json(partner);
            }
        );
    });
});

#1


3  

Since the products field in Partner model is an array that holds _id refs to the Product model, you are supposed to push an _id to the array, not an object hence Mongoose complains with an error.

由于Partner模型中的products字段是一个将_id refs保存到Product模型的数组,所以应该将_id推到数组中,而不是对象,因此Mongoose会报错。

You should restructure your code to allow the saving of the Product _id ref to the Partner model:

您应该重构代码,以便将产品_id ref保存到合作伙伴模型:

router.post('/addProduct', function (req, res) {
    var product = new campSchema.Product(req.body.dataProduct);

    product.save(function (err) {
        if (err) return throw err;        
        campSchema.Partner.findByIdAndUpdate(
            req.body.partnerId,
            { "$push": { "products": product._id } },
            { "new": true },
            function (err, partner) {
                if (err) throw err;
                res.json(partner);
            }
        );
    });
});