Problem registering nested objects in Mongodb with mongoose

1

I have the following collection:

    // SUSPEITOS - INICIO //
const suspeitosSchema = new mongoose.Schema({
  nome: { type: String },
  sexo: { type: String },
  corPele: { type: String },
  altura: { type: String },
  peso: { type: String },
  tamanhoCabelo: { type: String },
  corCabelo: { type: String }
})

// ACOES //
const acoesSchema = new mongoose.Schema({
  dataCadastro: { type: Date, default: Date.now },
  fonte: { type: String },
  bo: { type: String },
  numeroBo: { type: Number },
  imagem: { type: String },
  relato: { type: String },
  modus: { type: String },
  falhasApuradas: { type: String },
  dataOcorrencia: { type: Date }, 
  latitude: { type: String },
  longitude: { type: String },
  suspeitos: [suspeitosSchema]
})

I need to register a json that I get from a form via POST, registering it is, however, I'm using a multi-select ( link ) and the return of it is another json, as you can see below:

WhenIsubmittheformthroughPOST,itregisters,butthedatathatcamefromtheMultiselectJSONregistersasfollows,noticetheitems" source " and " suspects ":

{ 
    "_id" : ObjectId("5a04a17af7a3373fe007cbca"), 
    "fonte" : "[object Object],[object Object],[object Object]", 
    "eventosDeRisco" : [

    ], 
    "acoesCriminosas" : [

    ], 
    "alertas" : [

    ], 
    "veiculos" : [

    ], 
    "suspeitos" : [
        {
            "nome" : "[object Object],[object Object]", 
            "_id" : ObjectId("5a04a17af7a3373fe007cbcb")
        }
    ], 
    "dataCadastro" : ISODate("2017-11-09T18:42:02.842+0000"), 
    "__v" : NumberInt(0)
}
    
asked by anonymous 09.11.2017 / 19:40

1 answer

2

In your model, you have the following specification:

nome: { type: String }

However, you are sending an array of objects:

nome: [
  {name: "Super Homem", ticked: true},
  {name: "Batmam", ticked: true}
]

Set your schema so that nome contains an object instead of a string , or modify the payload on the client side before to send to the backend .

    
09.11.2017 / 20:05