验证失败:video_data.0._id:在路径 \"_ 处的值 \"\"(类型字符串)转换为 ObjectId 失败

Posted

技术标签:

【中文标题】验证失败:video_data.0._id:在路径 \\"_ 处的值 \\"\\"(类型字符串)转换为 ObjectId 失败【英文标题】:validation failed: video_data.0._id: Cast to ObjectId failed for value \"\" (type string) at path \"_验证失败:video_data.0._id:在路径 \"_ 处的值 \"\"(类型字符串)转换为 ObjectId 失败 【发布时间】:2022-01-16 18:59:42 【问题描述】:

我正在构建一个 React 表单,当我从 react 发出发布请求时,我收到错误消息验证失败:video_data.0.id: Cast to ObjectId failed for value "" (type string) at path " .这里我通过 console.log(JSON.stringify(booksStore)+"datahhhhhhhhhhhhhhhhhhh") 获取数据。这里使用两种动态形式。我该如何解决这个问题,请指导我

    
       "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%20%28166%29.png",
       "booksAudio":"https://booksaudioes.s3.amazonaws.com/Screenshot%20%28166%29.png",
       "video_data":[
          
             "_id":"61b6f8a3ba3b7a3d10d0afe1",
             "number_of_chapters":1,
             "video_title":"ghhghggg",
             "description":"ghghg",
             "booksVideo":"https://hashvideo.s3.ap-south-1.amazonaws.com/Screenshot%20%28166%29.png",
             "booksImage":"https://bookstoreimage.s3.amazonaws.com/Screenshot%20%28166%29.png",
             "releaseDate":null,
             "subvideo_data":[
                
                   "_id":"61b6f8a3ba3b7a3d10d0afe2",
                   "pageNo":1,
                   "subvideo_title":"hhghgghghgg",
                   "subdescription":"vfgv",
                   "booksVideo":"https://hashvideo.s3.ap-south-1.amazonaws.com/Screenshot%20%28166%29.png",
                   "booksImage":"https://bookstoreimage.s3.ap-south-1.amazonaws.com/Screenshot%20%28166%29.png",
                   "subreleaseDate":null
                ,
                
                   "_id":"61b6f8a3ba3b7a3d10d0afe3",
                   "subvideo_title":"hhghgghghgg",
                   "subdescription":"hgjhjh",
                   "pageNo":2,
                   "booksImage":"https://bookstoreimage.s3.ap-south-1.amazonaws.com/Screenshot%20%28166%29.png",
                   "booksVideo":"https://hashvideo.s3.ap-south-1.amazonaws.com/Screenshot%20%28166%29.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??

【问题讨论】:

【参考方案1】:

为解决此类问题,您需要验证您的代码,您的反应 this.State 中是否已经声明了任何 _id?如果您声明了任何与您的数据库响应相匹配的 _id ,例如 _id 然后转到您的代码,只需修改它或删除它,然后再试一次。这将是工作

【讨论】:

以上是关于验证失败:video_data.0._id:在路径 \"_ 处的值 \"\"(类型字符串)转换为 ObjectId 失败的主要内容,如果未能解决你的问题,请参考以下文章

对于模型的路径“_id”处的值,转换为 ObjectId 失败

Mongoose:'投射到嵌入的路径值失败。无法使用“in”运算符搜索“_id”

模型“Coach”的路径“_id”处的值“”转换为 ObjectId 失败

Mongoose:CastError:在路径“_id”处为值“[object Object]”转换为 ObjectId 失败

CastError:模型“Company”的路径“_id”处的值“...”转换为 ObjectId 失败

CastError:模型“Company”的路径“_id”处的值“...”转换为 ObjectId 失败