Después de actualizar a Mongoose 5.11.13, recibo el siguiente error cuando intento agregar un elemento a un subobjeto dentro de un documento.
CastError: Cast to embedded failed for value "{ value: 'new item' }" at path "items" at model.Query.exec (D:\repos\pushbox\node_modules\mongoose\lib\query.js:4358:21) at model.Query.Query.then (D:\repos\pushbox\node_modules\mongoose\lib\query.js:4452:15) at processTicksAndRejections (internal/process/task_queues.js:97:5) { messageFormat: undefined, stringValue: `"{ value: 'new item' }"`, kind: 'embedded', value: "{ value: 'new item' }", path: 'items', reason: TypeError: this.ownerDocument(...).isSelected is not a function
Mi Schma principal se llama Card
. Contiene un subobjeto/subdocumento llamado Property
y se ve así:
export const CardSchema = new mongoose.Schema({ title: { type: String, required: true, }, description: { type: String, default: '', }, // Checklists in a Card checklists: [{ title: { type: String, required: true, }, items: [{ name: String, select: Boolean, }], }], // Properties in a card properties: [{ name: { type: String, required: true, }, items: [{ value: { type: String, default: '' }, isSelected: { type: Boolean, default: false }, }], }], box: { type: ObjectId, ref: 'Box', }, }, { timestamps: { createdAt: true, updatedAt: true }, });
La consulta que se utiliza para insertar un nuevo item
dentro de una property
es:
const newPropItem = await Card.findOneAndUpdate( { _id: cardId, 'properties._id': propertyId, }, { $push: { 'properties.$.items': { value: newItem.trim() }, }, }, { new: true, }, );
No tengo idea de por qué sucede esto, ya que tenemos una consulta similar para la Lista de Checklist
y funciona. Intenté esta consulta dentro del shell de mongo y funcionó allí. ¿Podrían ayudarme a averiguar qué me estoy perdiendo exactamente?
Ah, e intenté investigar todo el TypeError: this.ownerDocument(...).isSelected is not a function
, no tuve suerte para encontrar nada que pudiera ayudarme en mi caso
no puede usar isSelected
en Schema como nombre de campo porque isSelected()
internamente para verificar qué rutas necesitamos validar en mongoose, así que cambie el nombre del archivo a isSelect
o ...