Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to insert array of object in mongoose ExpressJs [ Solved ]

I want to insert any product that will have some info and different size with price. I can insert all other data but array of object(size with price) is not storing in mongo.

Here is my Product Schema:

const productSchema = new mongoose.Schema({
    product_name: {type:String, required: true},
    product_desc: {type: String, required:true},
    prod_cat: {type: mongoose.Schema.Types.ObjectId,ref: 'Category'},
    prod_img: {type: String},
    pro_price: [{
        prod_size: {type: String,},
        prod_price: {type: String}
    }]
},{timestamps: true})
const prodSchema = mongoose.model('Product',productSchema)

this is my controller to post that data:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 async postProduct(req,res){
            try{
                
                const newProduct = new prodSchema({
                    product_name: req.body.productName,
                    product_desc: req.body.productDetails,
                    prod_cat: req.body.category,
                    pro_price: req.body.pro
                })
                
                console.log(newProduct);
                const addProduct = newProduct.save()
                
            }
            catch(err){
                console.log(err);
            }
        }

I have sent data from a form and the object looks like this:

{
  productName: 'Margerita',
  productDetails: 'Served with onion, garlic sauce, meat slice, white sauce',
  category: '63221514e6b0279b001b9187',
  pro: [
    { size: 'Medium', price: '750' },
    { size: 'Big', price: '900' },
    { size: 'Small', price: '500' }
  ]
}

The data is storing in DB like this::( here note that all other data is storing except the array of object i’ve passed ) . Seems like i have messed while mapping (suggest me to map my re.body.pro array)..

MongoDb stored that object liek this:

{"_id":{"$oid":"632372b5c1d5d998a2db49ae"},"product_name":"Margerita","product_desc":"Served with onion, garlic sauce, meat slice, white sauce","prod_cat":{"$oid":"63221619fc117e0cee38d855"},"pro_price":[{"_id":{"$oid":"632372b5c1d5d998a2db49af"}},{"_id":{"$oid":"632372b5c1d5d998a2db49b0"}},{"_id":{"$oid":"632372b5c1d5d998a2db49b1"}}],"createdAt":{"$date":{"$numberLong":"1663267509614"}},"updatedAt":{"$date":{"$numberLong":"1663267509614"}},"__v":{"$numberInt":"0"}}

>Solution :

Most likely because your price schema (pro_price) expects prod_size and prod_price but the data in your example uses size and price. Try changing schema or data to use the same name.

For example, you could send the following data

{
  productName: 'Margerita',
  productDetails: 'Served with onion, garlic sauce, meat slice, white sauce',
  category: '63221514e6b0279b001b9187',
  pro: [
    { prod_size: 'Medium', prod_price: '750' },
    { prod_size: 'Big', prod_price: '900' },
    { prod_size: 'Small', prod_price: '500' }
  ]
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading