0

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?

1 Answer 1

0

Schema look like

name:{
    type: String,
    required: true // <---- 
    }
Sign up to request clarification or add additional context in comments.

1 Comment

So this is also another syntax..Both are correct. Unfortunately, the poblem doesn't lie in the schema definition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.