尝试将对象推送到子文档数组时出现此错误。
这是我的模特:
let imagemSchema = new mongoose.Schema({
titulo: String,
descricao: String,
arquivo: String
})
let imovelSchema = new mongoose.Schema({
imagens: [imagemSchema]
})
module.exports = mongoose.model('Imovel', imovelSchema)
当我尝试找到一个Imovel对象并将Imagem推送到它的imagens属性时,我收到以下错误:
无法读取未定义的属性'baseCasterConstructor'
我开始认为这可能是猫鼬本身的问题。
有人能帮助我吗?
提前致谢
答案 0 :(得分:0)
我找到了解决方案! 问题与我试图将对象推送到子数组的方式有关。
这篇文章pushing object into array schema in Mongoose 帮助我解决了改变我试图推动的方式
let findImovel = imovelModel.findById(req.params.id).exec()
findImovel.then(imovel => {
let imagem = req.body
let arquivo = req.file.filename
imagem.arquivo = arquivo
imovel.imagens.push(imagem)
要
let imagem = req.body
let arquivo = req.file.filename
imagem.arquivo = arquivo
imovelModel.findByIdAndUpdate(req.params.id,
{$push: {'imagens' : imagem}},
{safe: true, upsert: true}).then(() => {
return res.send()
})
它像我期待的那样起作用