Updating a subcollection on Mongodb

Asked

Viewed 16 times

0

I have a Collection called purchase with various data, among them:

const purchase = new Schema({
    datePurchase:{type: Date, default: Date.now()},
    ...
    products:[{
        product: {type: String},
        category: {type: String},
        price: {type: Number},
        additional:{ type: Array}, //Dados do adicional
        observation:{ type: String},
    }],
    ...
}

I made a controller to update the subcollection products, I’ll still have to exclude:

async updateProduct(req,res){
        await purchaseModel.update({
            id: req.params._id, 
            idProduct: req.params.products._id
        }, 
        req.body,
        {new: true})
        .then(response =>{
            return res.status(200).json(response)
        })
        .catch(error =>{
            return res.status(500).json(error)
        })
    }

However, when I simulate the update, I get the following error:

Unhandledpromiserejectionwarning: Typeerror: Cannot read Property '_id' of Undefined

That one _id refers clearly to the id of products.

I couldn’t find a suitable solution. I would like help formulating an update and an exclusion that works.

  • A doubt, this statement of the new Schema? The object should not be inside the new Schema({...})?

  • Is like Schema({...}), i who got it wrong. I changed the post.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.