mongodb - Mongoose CastError String -


i've been having problem code. i'm using mongoose connect mongodb , store documents. i'm trying store calendar events object array. when send data server shows error:

{ [casterror: cast string failed value "[object object]" @ path "agenda"]  message: 'cast string failed value "[object object]" @ 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 } } 

if turn string works, in model not array of strings.

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 code

camaramodel.findbyidandupdate({       _id: req.body._id     }, {       $push: {           'agenda': req.body.agenda       }     }, {       setdefaultsoninsert: true     }, function(err, users) {       console.log(err);       res.json(users);     }); 

i have experienced same problem, think has how declare subdocument (agenda). instead of setting object agenda array (agenda: [{...}]), documentation:

var childschema = new schema({ name: 'string' });  var parentschema = new schema({     children: [childschema] }) 

so in case:

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],     ... }); 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -