2016-04-19 63 views
0

我一直有我的代码有问题。我使用猫鼬与mongoDB连接并存储文档。我试图将日历事件存储到对象数组中。但是当我将数据发送到服务器时,它显示了这个错误:猫鼬CastError字符串

{ [CastError: Cast to string failed for value "[object Object]" at path "agenda"] 
message: 'Cast to string failed for value "[object Object]" at path "agenda"', 
name: 'CastError', 
kind: 'string', 
value: 
    { type: 'Sessão Plenária', 
    vereadores: [Object], 
    local: [Object], 
    desc: 'SS', 
    nome: 'Novo Evento', 
    data_horafim: '2016-04-20T07:00:00.000Z', 
    data_horainicio: '2016-04-19T19:00:00.000Z' }, 
path: 'agenda', 
reason: undefined } } 

,如果我把它变成它的工作原理字符串,但在我的模型是不是字符串数组。

var camaraSchema = new mongoose.Schema({ 
obj_id_usuario: { 
    type: String, 
    default: '' 
}, 

cidade: String, 
estado: String, 

endereco: { 
    lbl_logradouro: { 
     type: String, 
     default: '' 
    }, 
    lbl_numero: { 
     type: String, 
     default: '' 
    }, 
    lbl_complemento:{ 
     type:String, 
     default:'', 
    }, 
    lbl_bairro: { 
     type: String, 
     default: '' 
    }, 
    lbl_cep: { 
     type: String, 
     default: '' 
    }, 
}, 

redesocial: { 
    siteoficial: { 
     type: String, 
     default: '' 
    }, 
    twitter: { 
     type: String, 
     default: '' 
    }, 
    facebook: { 
     type: String, 
     default: '' 
    }, 
    email: { 
     type: String, 
     default: '' 
    } 
}, 

agenda:[{ 
    data_horainicio: Date, 
    data_horafim: Date, 
    nome:String, 
    desc:String, 
    local:{ 
    logradouro: String, 
    numero: String, 
    complemento: String, 
    bairro: String, 
    cidade: String, 
    estado: String, 
    cep: String 
    }, 
    vereadores:[String], 
    type: String 
}], 

mesaDiretora:{ 
    vereadores:[{ 
    vereador: String, 
    funcao: String, 
    }], 
    funcoes:[String] 
}, 

setores:[{ 
    nome: String, 
    telefone: String, 
    email: String 
}], 

sessao:[{ 
    numero: String, 
    tipo: String, 
    datahora: Date, 
    arquivo: String, 
}], 

tipoSessao:[{ 
    codigo: String, 
    desc: String, 
}], 

lideranca:[{ 
    desc: String, 
    vereadores: [String], 
    lider: String 
}], 

noticias:[{ 
    titulo: String, 
    assesor: String, 
    datahora: Date, 
    corpo: String, 
    imagem: String, 
}], 
imagens:[{ 
    titulo: String, 
    foto: String 
}], 

contrato: { 
    obj_id_representante: { 
     type: String, 
     default: '' 
    }, 
    int_contrato: { 
     type: String, 
     default: '' 
    }, 
    int_empenho: { 
     type: String, 
     default: '' 
    }, 
    lbl_licitacao: { 
     type: String, 
     default: '' 
    }, 

    dt_inicio: Date, 

    lbl_periodo: { 
     type: String, 
     default: '' 
    }, 
    ft_valor: { 
     type: Number, 
     default: 0 
    }, 
    pc_representante: { 
     type: String, 
     default: '' 
    }, 
    lbl_observacao: { 
     type: String, 
     default: '' 
    }, 
    arq_contrato: { 
     type: String, 
     default: '' 
    }, 
}, 

telefone: { 
    type: String, 
    default: '' 
}, 
bl_ativo: { 
    type: Boolean, 
    default: false 
}, 
lbl_img: { 
    type: String, 
    default: 'default-user.png' 
}, 
ts_data: { 
    type: Date, 
    default: Date.now 
}, 

}

代码的NodeJS

CamaraModel.findByIdAndUpdate({ 
     _id: req.body._id 
    }, { 
     $push: { 
      'agenda': req.body.agenda 
     } 
    }, { 
     setDefaultsOnInsert: true 
    }, function(err, users) { 
     console.log(err); 
     res.json(users); 
    }); 

回答

0

我已经经历了同样的问题,我认为这与您如何声明您子(议程)做。相反设置对象纳入重要议事日程阵列(agenda: [{...}])的,不喜欢的documentation

var childSchema = new Schema({ name: 'string' }); 

var parentSchema = new Schema({ 
    children: [childSchema] 
}) 

所以你的情况:

var AgendaSchema = new mongoose.Schema({ 
    data_horainicio: Date, 
    data_horafim: Date, 
    nome:String, 
    desc:String, 
    local:{ 
     logradouro: String, 
     numero: String, 
     complemento: String, 
     bairro: String, 
     cidade: String, 
     estado: String, 
     cep: String 
    }, 
    vereadores:[String], 
    type: String 
}); 

var camaraSchema = new mongoose.Schema({ 
    ..., 
    agenda: [AgendaSchema], 
    ... 
});