0

I want to pass my array of string to my schema. As you can see in auth.js image:image here I am passing my image array to my mongodb property image and i am getting array of strings successfully in image but I tried everything like $push, $each but i don't know how to pass the image array to my Mongodb schema. I hope you understand my query.

Schema.js

const mongoose=require('mongoose');

const mySchema=mongoose.Schema({
    
    image:{
        type:[String],
        required:true,
        value:[String]
    }
    
})


const MyCattle=mongoose.model('cattle_list',mySchema);

auth.js

const finaldata = new MyCattle({
        image:[image]
    })
    
    module.exports=MyCattle;
2
  • Does this answer your question? Store images in a MongoDB database Commented May 29, 2021 at 5:07
  • I don't want to store images. I want to pass array of strings which contains images names. Commented May 29, 2021 at 8:20

1 Answer 1

0

I believe this is a duplicate of this question, but I shall share the answer here:

const mySchema = mongoose.Schema({
    image: {
        type: [{ type: String }]
    }
});

// ...
Sign up to request clarification or add additional context in comments.

1 Comment

I tried your code but it still showing me the error i.e ( ValidationError: image.0: Cast to [string] failed for value "[{"image":["sprat (2).jpg"]}]" at path "image.0" )

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.