Home > OS >  validation failed: video_data.0._id: Cast to ObjectId failed for value \"\" (type string
validation failed: video_data.0._id: Cast to ObjectId failed for value \"\" (type string

Time:12-15

im building a React form ,when i make the post request from react then im getting error message validation failed: video_data.0.id: Cast to ObjectId failed for value "" (type string) at path " .here im getting data by console.log(JSON.stringify(booksStore) "datahhhhhhhhhhhhhhhhhhh") like this.here is useing two dynamic form.how can i solve this issue please guide me

    {
       "book_rating_count":22,
       "book_review":0,
       "book_like":0,
       "_id":"61b6f8a3ba3b7a3d10d0afe0",
       "book_title":"dgfgh",
       "book_authore":"uytiu",
       "book_overview":"fghfjhhg",
       "book_category":"jhk",
       "book_genre":"Trending Now",
       "book_tag":"comedy",
       "book_languages":"uii",
       "book_duration":"uiuy",
       "book_rating":5,
       "book_publication":"gyhrt",
       "booksEpdf":"https://booksepdf.s3.amazonaws.com/Screenshot (166).png",
       "booksAudio":"https://booksaudioes.s3.amazonaws.com/Screenshot (166).png",
       "video_data":[
          {
             "_id":"61b6f8a3ba3b7a3d10d0afe1",
             "number_of_chapters":1,
             "video_title":"ghhghggg",
             "description":"ghghg",
             "booksVideo":"https://hashvideo.s3.ap-south-1.amazonaws.com/Screenshot (166).png",
             "booksImage":"https://bookstoreimage.s3.amazonaws.com/Screenshot (166).png",
             "releaseDate":null,
             "subvideo_data":[
                {
                   "_id":"61b6f8a3ba3b7a3d10d0afe2",
                   "pageNo":1,
                   "subvideo_title":"hhghgghghgg",
                   "subdescription":"vfgv",
                   "booksVideo":"https://hashvideo.s3.ap-south-1.amazonaws.com/Screenshot (166).png",
                   "booksImage":"https://bookstoreimage.s3.ap-south-1.amazonaws.com/Screenshot (166).png",
                   "subreleaseDate":null
                },
                {
                   "_id":"61b6f8a3ba3b7a3d10d0afe3",
                   "subvideo_title":"hhghgghghgg",
                   "subdescription":"hgjhjh",
                   "pageNo":2,
                   "booksImage":"https://bookstoreimage.s3.ap-south-1.amazonaws.com/Screenshot (166).png",
                   "booksVideo":"https://hashvideo.s3.ap-south-1.amazonaws.com/Screenshot (166).png",
                   "subreleaseDate":"2021-12-13T07:39:15.412Z"
                }
             ]
          }
       ],
       "book_created_date":"2021-12-13T07:39:15.414Z"
    }
here is my mongoose schemawhich im using to save it.
const mongoose = require('mongoose')


const BooksStoreSchema = mongoose.Schema({
    
    book_title : {
         type: String,
          required:[true,"enter your book name"] 
        },

    book_authore: { 
        type: String,
        required:[true,"enter your book author name"] 
         },
    
    book_overview: {
         type: String,
           required:[true,"enter your book description"] 
        }, 
    book_publication: {
         type: String,
           required:[true,"enter your book publication name"] 
        },
    book_category: {
        type: String,
        required:[true,"enter book category name"] 
           }, 
           
    book_genre: {
        type: String,
        enum: ["Feel Good Novels", "Trending Now", "Fiction", "Best sellers", "Originals","Internationals"]
           }, 
           
    book_tag: {
        type: String,
        required:true,
        enum: ["comedy","thriller","horror"]
           }, 
    book_rating_count :{
        type:Number,
        default:0
    },
    book_languages :{
        type:String,
        required:true
    },
    book_duration :{
        type:String,
        required:true
    },
    book_rating :{
        type:Number,
        required:true
    },
    booksEpdf:{
        type:String,
        required:true
    },
    booksAudio:{
        type:String,
        required:true
    },
    video_data: [{
                 
                number_of_chapters:{
                    type:Number,
                    required:true,
                },
                video_title: {
                    type:String,
                    required:true
                },
                description:{
                    type:String,
                    required:true
                },
                booksVideo:{
                    type:String,
                    required:true
                },
                booksImage:{
                    type:String,
                    required:true

                },
                releaseDate:{
                      type: Date,
                      default: Date.now,
                },
                subvideo_data: [{
                        pageNo:{
                            type:Number,
                            required:true
                        },
                        
                        subvideo_title:{
                            type:String,
                            required:true
                        },
                        subdescription:{
                            type:String,
                            required:true
                        },
                        booksVideo: {
                            type:String,
                            required:true
                        },
                        booksImage: {
                            type:String,
                            required:true
                        },
                        subreleaseDate:{
                             type: Date,
                             default: Date.now,
                        },
                    },
                ],
            }],
    //like view and created schema..... 
    book_review: {
        type: Number,
        default: 0,
    },

    book_like: {
        type:Number,
        default:0
    },
    book_created_date: {
        type: Date,
        default: Date.now,
    },

});
module.exports = mongoose.model('booksStore', BooksStoreSchema)
 here is my controller....
router.post('/',    function(req, res,next) {

  const booksStore = new BooksStore({
            book_title : req.body.book_title,
            book_authore : req.body.book_authore,
            book_overview : req.body.book_overview,
            book_category : req.body.book_category,
            book_genre :req.body.book_genre,
            book_tag : req.body.book_tag,
            book_languages: req.body.book_languages,
            book_duration : req.body.book_duration,
            book_rating : req.body.book_rating,
            book_publication : req.body.book_publication,
            book_rating_count:req.body.book_rating_count,
            booksEpdf:  req.body.booksEpdf,
            booksAudio: req.body.booksAudio,
            video_data:req.body.video_data,
            book_review:req.body.book_review,
            book_like:req.body.book_like,
            book_created_date:req.body.book_created_date,
           
        });
        console.log(JSON.stringify(booksStore) "datahhhhhhhhhhhhhhhhhhh")
        booksStore.save()
            .then(result => {
                res.status(200).send({
                    _id: result._id,
                     message:"submit successfully"
                })
            })
            .catch(err => {
                res.send({ message: err })
            })
            })
    

    actully i was used dynamic form in react, i want to save this data,please help me how can i solve this issue??

CodePudding user response:

for solve this kind of issue you need to verifyyour code ,is any _id already declare in your react this.State?? if you have declared any _id which do match your database response like this _id then go to your code simply modify it or remove it then try it again. that will be work

  • Related