2017-09-22 5 views
0

mongooseデータベースにデータを保存する際に問題があります。私のモデルとコントローラのすべての情報を見つけることができます。問題は、特定の投稿が好きなユーザーの配列を保持したいため、ObjectIdを配列に保持していることと、モデルに[{ type: Schema.Types.ObjectId, ref: 'User' }]と指定していることです。GIFの検証に失敗しました:likes: "likes"のパスで値が "1"の場合、配列にキャストできません

私は前に、データを操作した後にログインして、それは私に所望の出力を与える:あなたの場合お願い申し上げ

ValidationError: Gif validation failed: likes: Cast to Array failed for value "1" at path "likes" 

The likes value of the found gif: [] 
Document Id is: 59c3122632af313ff3a9d962 
User not liked yet! 
New likes value: ["59c3122632af313ff3a9d962"] 

それでも、それは私に、次の検証エラーを与える伝えます私のコードで何が間違っているかを指摘する。ベスト!ここで

は私のコントローラである:ここでは

export function handleLike(req, res) { 
    // We will need the uid of the liking user and cuid of the liked gif 
    Gif.findOne({ cuid: req.body.cuid }).exec((err, gif) => { 
    if (err) { 
     res.status(500).send(err) 
     console.log('Error while the gif is being found.') 
    } 
    console.log('The likes value of the found gif: ', gif.likes) 
    User.findOne({ uid: req.body.uid }).exec((e, user) => { 
     if (e) { 
     res.status(500).send(err) 
     console.log('Error while user is being found.') 
     } 
     const userDocumentId = user._id 
     console.log('Document Id is: ', userDocumentId) 
     const isUserLiked = gif.likes.includes(userDocumentId) 
     console.log('User not liked yet!') 
     if (isUserLiked) { 
     const newLikeArray = gif.likes.filter(like => like !== userDocumentId) 
     gif.set({ likes: newLikeArray }) 
     } else { 
     const newLikeArray = gif.likes.push(userDocumentId) 
     gif.set({ likes: newLikeArray }) 
     } 
     console.log('New likes value: ', gif.likes) 
     gif.save((error, newGif) => { 
     console.log(gif) 
     if (error) { 
      res.status(500).send(error) 
      console.log('Error while saving the gif.') 
      console.log(error) 
     } 
     res.send(newGif) 
     }) 
    }) 
    }) 
} 

は私のモデルである:下記のよう

const GifSchema = Schema({ 
    // In case there may occur a problem with Google Cloud Upload, make URL required! 
    id: { type: String, required: true }, 
    crop_start: { type: Number, required: true }, 
    crop_finish: { type: Number, required: true }, 
    meme: String, 
    tags: [String], 
    url: { type: String }, 
    cuid: { type: 'String' }, 
    uploaded: { type: Date, default: Date.now }, 
    disabled: { type: Boolean, default: false }, 
    likes: [{ type: Schema.Types.ObjectId, ref: 'User' }], 
    owner: { type: Schema.Types.ObjectId, ref: 'User' }, 
    customWidth: Number, 
    customHeight: Number, 
}) 

そして、コンソール出力とエラーは次のとおりです。

The likes value of the found gif: [] 
Document Id is: 59c3122632af313ff3a9d962 
User not liked yet! 
New likes value: ["59c3122632af313ff3a9d962"] 
{ _id: 59c3d98b65fcef2f9ad60230, 
    owner: 59c3122632af313ff3a9d962, 
    customHeight: 180, 
    customWidth: 320, 
    url: 'http://res.cloudinary.com/de9nq41ka/video/upload/v1506007487/js4pnuezelrtitxnbovd.mp4', 
    cuid: 'cj7ulxyg40000eixhum3hjqbr', 
    id: 'gmn1no0lEuk', 
    crop_start: 0.35503994850158693, 
    crop_finish: 2.407723893188477, 
    meme: '', 
    __v: 0, 
    likes: [ 59c3122632af313ff3a9d962 ], 
    disabled: false, 
    uploaded: 2017-09-21T15:23:55.653Z, 
    tags: [ 'hayvan', 'tatlı', 'komik', 'eglenceli' ] } 
Error while saving the gif. 
{ ValidationError: Gif validation failed: likes: Cast to Array failed for value "1" at path "likes" 
    at ValidationError.inspect (/home/ugur/Desktop/gifl.io/react-webpack/node_modules/mongoose/lib/error/validation.js:57:23) 
    at formatValue (util.js:357:36) 
    at inspect (util.js:221:10) 
    at format (util.js:98:24) 
    at Console.log (console.js:127:21) 
    at /home/ugur/Desktop/gifl.io/react-webpack/src/server/controllers/gif.controller.js:101:19 
    at /home/ugur/Desktop/gifl.io/react-webpack/node_modules/mongoose/lib/model.js:3835:16 
    at /home/ugur/Desktop/gifl.io/react-webpack/node_modules/mongoose/lib/services/model/applyHooks.js:167:17 
    at _combinedTickCallback (internal/process/next_tick.js:131:7) 
    at process._tickDomainCallback (internal/process/next_tick.js:218:9) 
    errors: 
    { likes: 
     { CastError: Cast to Array failed for value "1" at path "likes" 
      at CastError (/home/ugur/Desktop/gifl.io/react-webpack/node_modules/mongoose/lib/error/cast.js:27:11) 
      at model.Document.set (/home/ugur/Desktop/gifl.io/react-webpack/node_modules/mongoose/lib/document.js:766:7) 
      at model._handleIndex (/home/ugur/Desktop/gifl.io/react-webpack/node_modules/mongoose/lib/document.js:598:14) 
      at model.Document.set (/home/ugur/Desktop/gifl.io/react-webpack/node_modules/mongoose/lib/document.js:558:24) 
      at /home/ugur/Desktop/gifl.io/react-webpack/src/server/controllers/gif.controller.js:93:13 
      at /home/ugur/Desktop/gifl.io/react-webpack/node_modules/mongoose/lib/query.js:2922:18 
      at <anonymous> 
      at process._tickDomainCallback (internal/process/next_tick.js:228:7) 
     message: 'Cast to Array failed for value "1" at path "likes"', 
     name: 'CastError', 
     stringValue: '"1"', 
     kind: 'Array', 
     value: 1, 
     path: 'likes', 
     reason: [Object] } }, 
    _message: 'Gif validation failed', 
    name: 'ValidationError' } 

答えて

0

あなたは渡していますあなたが反応を使っているかのように、間違った議論。setState

gif.set({ likes: newLikeArray }) 

APIによると、あなたは別にpathvalueを渡す必要は:

Document#set(path, val, [type], [options]) 

Sets the value of a path, or many paths.
Parameters:
path path or object of key/vals to set
val the value to set
[type] optionally specify a type for "on-the-fly" attributes
[options] optionally specify options that modify the behavior of the set

のいずれかを使用:

gif.set('likes', newLikeArray) 

か:私は同じエラーを与える

gif.likes = newLikeArray 
+0

次のウェブサイトでは、 t()関数は、私が使用した方法で使用されます。それは奇妙です...それを確認してください:http://mongoosejs.com/docs/documents.html |彼らはtank.set({size: 'large'})として使用しました。 –

関連する問題