Swati
Swati

Reputation: 1

Why is insertMany function throwing error?

I am trying to add in details onto my mongodb cluster. Here is how I am trying to do:

async function insertDummyCategoryData(){

try{
    await Category.insertMany([
        {
            "name": "Thai",
            "image": "1.jpg"
        },
        {
            "name": "Indian",
            "image": "1.jpg"
        },
        {
            "name": "Chinese",
            "image": "1.jpg"
        },
        {
            "name": "American",
            "image": "1.jpg"
        },
        {
            "name": "Mexican",
            "image": "1.jpg"
        },
        {
            "name": "Continental",
            "image": "1.jpg"
        },
    ]);

} catch(error){
    console.log('Error in adding details to the DB ', error);
}

}

insertDummyCategoryData();

//Category Schema --------------------------------->

const mongoose = require('mongoose');

const categorySchema = new mongoose.Schema({

name: {
    type: String,
    required: 'This field is required.'
},

image: {
    type: String,
    required: 'This field is required.'
}

});

module.exports = mongoose.model('Category' , categorySchema);

But I am getting the below error:

C:\Users\Node JS\Personal\Recipe Blog\node_modules\mongoose\lib\model.js:3152 for (let i = 0; i < error.writeErrors.length; ++i) { ^

TypeError: Cannot read properties of undefined (reading 'length') at C:\Users\Node JS\Personal\Recipe Blog\node_modules\mongoose\lib\model.js:3152:47

I am new to programming. Not able to figure why. Any help is much appreciated!

I tried looking up where the suggestion was to change the localhost connection to xxx.x.x.x.

Here is how it is in my file:

mongoose.connect(process.env.MONGODB_URI, {useNewUrlParser: true, useUnifiedTopology: true});.

How to do it?

Upvotes: 0

Views: 107

Answers (1)

Dhanushka Dayawansha
Dhanushka Dayawansha

Reputation: 356

Schema look like

name:{
    type: String,
    required: true // <---- 
    }

Upvotes: 0

Related Questions