skip to Main Content

I am working on a node js project where I am getting the following error :

CastError: Cast to ObjectId failed for value "[ [ '63e8e6e8b8a285d68b1fbd0c', '63e9951ded54221a516fc622' ] ]" (type Array) at path "labels" because of "BSONTypeError"
    at ObjectId.cast (/var/www/html/IssueTracker/node_modules/mongoose/lib/schema/objectid.js:248:11)
    at ObjectId.SchemaType.applySetters (/var/www/html/IssueTracker/node_modules/mongoose/lib/schematype.js:1201:12)
    at Proxy._cast (/var/www/html/IssueTracker/node_modules/mongoose/lib/types/array/methods/index.js:245:43)
    at Proxy._mapCast (/var/www/html/IssueTracker/node_modules/mongoose/lib/types/array/methods/index.js:258:17)
    at Arguments.map (<anonymous>)
    at Proxy.push (/var/www/html/IssueTracker/node_modules/mongoose/lib/types/array/methods/index.js:675:21)
    at module.exports.createissue (/var/www/html/IssueTracker/controllers/issuecontroller/issuecontroller.js:121:27)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  stringValue: `"[ [ '63e8e6e8b8a285d68b1fbd0c', '63e9951ded54221a516fc622' ] ]"`,
  messageFormat: undefined,
  kind: 'ObjectId',
  value: [ [ '63e8e6e8b8a285d68b1fbd0c', '63e9951ded54221a516fc622' ] ],
  path: 'labels',
  reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
      at new BSONTypeError (/var/www/html/IssueTracker/node_modules/bson/lib/error.js:41:28)
      at new ObjectId (/var/www/html/IssueTracker/node_modules/bson/lib/objectid.js:67:23)
      at castObjectId (/var/www/html/IssueTracker/node_modules/mongoose/lib/cast/objectid.js:25:12)
      at ObjectId.cast (/var/www/html/IssueTracker/node_modules/mongoose/lib/schema/objectid.js:246:12)
      at ObjectId.SchemaType.applySetters (/var/www/html/IssueTracker/node_modules/mongoose/lib/schematype.js:1201:12)
      at Proxy._cast (/var/www/html/IssueTracker/node_modules/mongoose/lib/types/array/methods/index.js:245:43)
      at Proxy._mapCast (/var/www/html/IssueTracker/node_modules/mongoose/lib/types/array/methods/index.js:258:17)
      at Arguments.map (<anonymous>)
      at Proxy.push (/var/www/html/IssueTracker/node_modules/mongoose/lib/types/array/methods/index.js:675:21)
      at module.exports.createissue (/var/www/html/IssueTracker/controllers/issuecontroller/issuecontroller.js:121:27),
  valueType: 'Array'
}

I have 2 documents one is issue and other is a label. Issue has one to many relation with the label schema. so my schema for label looks like this

 labels:[{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Label'
    }],

the issue is i don’t get the error when i insert a single value in the labels using this
code

 issues.labels.push(allLabelsWithId);

here allLabelsWithId is a array. But this array when have value more than 1 it fails and give the error mentioned above.

I need to pass multiple values so what is the correct way of doing it.

2

Answers


  1. Try with:

    issues.labels.push(...allLabelsWithId);
    
    Login or Signup to reply.
  2. For multiple values, one solution is the $each operator

    Issue.findByIdAndUpdate(yourId, {
      $push: { labels: { $each: allLabelsWithId } },
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search