Я работаю над проектом node js, где получаю следующую ошибку:
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'
}
У меня есть 2 документа, один выпускной, а другой этикетка. Задача имеет отношение один ко многим со схемой метки. поэтому моя схема для ярлыка выглядит так
labels:[{
type:mongoose.Schema.Types.ObjectId,
ref:'Label'
}],
проблема в том, что я не получаю ошибку, когда вставляю одно значение в метки, используя это код
issues.labels.push(allLabelsWithId);
здесь allLabelsWithId — это массив. Но этот массив, когда имеет значение больше 1, терпит неудачу и дает ошибку, упомянутую выше.
Мне нужно передать несколько значений, так как это правильно сделать.
да, но concat не добавляет элементы в поля меток коллекции.
Попробуйте с:
issues.labels.push(...allLabelsWithId);
Для нескольких значений одним из решений является оператор $each.
Issue.findByIdAndUpdate(yourId, {
$push: { labels: { $each: allLabelsWithId } },
});
Вы пробовали concat вместо
push