Reputation: 92
I'm getting error whenever I'm trying to save into my database a document with this schema:
var schemaForBooks = new Schema({
book: String,
author: String,
who_has_this: Object,
points: Number,
upvoted_by_users: [Schema.Types.ObjectId],
downvoted_by_users: [Schema.Types.ObjectId]
});
Rest all is good, but putting anything into upvoted_by_users
or downvoted_by_users
, I get this error:
[ERROR] Trace- CastError: Cast to [ObjectId] failed for value "[{"userName":"Vibhu","userId":"3833d1g870feaf4a38723"}]" at path "upvoted_
by_users"
I'm pretty sure I'm doing something wrong with the schema itself, but I don't know what.
Any help would be appreciated.
Upvotes: 2
Views: 1341
Reputation: 5544
The error says that you're trying to cast the following array of object {"userName":"Vibhu", "userId":"3833d1g870feaf4a38723"}
to an array of ObjectId
.
So, you need to get rid of the userName
field, so you can turn your array of String
+ ObjectId
to an array of ObjectId
.
You can do it with the Array#map
method on your js Array, for example :
var arr = [{"userName":"Vibhu","userId":"3833d1g870feaf4a38723"}];
var myCorrectArr = arr.map(field => field.userId);
Hope it helps,
Best regards,
Upvotes: 1