我在将一些数据保存到mongoose数据库时遇到了麻烦。您可以找到我的模型和控制器的所有信息。问题是我希望保留一组喜欢特定帖子的用户,因此我将他们的ObjectId保存在数组中,另外我在模型中指定为[{ type: Schema.Types.ObjectId, ref: 'User' }]
。
我在操作数据之前和之后记录,它给了我想要的输出:
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 :(得分:0)
您传递的是错误的参数,就好像您正在使用react的setState
:
gif.set({ likes: newLikeArray })
根据API,您应该单独传递path
和value
:
Document#set(path, val, [type], [options])
设置路径或多条路径的值 参数:
行为的选项
路径路径或键/对象设置
val要设置的值
[type]可选择指定“on-the-fly”属性的类型
[options]可选择指定修改集合
使用:
gif.set('likes', newLikeArray)
或:
gif.likes = newLikeArray