2017-09-22 32 views
0

我在保存一些數據到貓鼬數據庫時遇到了麻煩。你可以找到我的模型和控制器的所有信息。問題是我想保留一個喜歡特定帖子的用戶數組,因此我在數組中保存了它們的ObjectId,另外我在模型中指定了[{ type: Schema.Types.ObjectId, ref: 'User' }]Gif驗證失敗:喜歡:投射陣列失敗,值「1」,路徑爲「likes」

我之前和處理後的數據日誌的,它給了我想要的輸出:

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

儘管如此,它告訴給我下面的驗證錯誤:

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

將不勝感激,如果你指出我的代碼中有什麼問題。最好!

這裏是我的控制器:

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

你傳入錯誤的論點,就好像你正在使用react的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'}); –