Problem registering objects nested in Mongodb with Mongoose

Asked

Viewed 123 times

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 receive from a form via POST, registering is, however, I am using a multi-select (http://isteven.github.io/angular-multi-select/#/main) and its return is another json, as you can see below:

inserir a descrição da imagem aqui

When sending the form through the POST, register, but the data that came from JSON Multiselect register as follows, notice the items "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)
}
  • 2

    You’ll need to do the same thing you did to suspects at source and I noticed you’re coming name instead of nome that was modeled.

  • name is a multiselect property, I can’t change it. I can’t understand the right way to get this JSON to register correctly.

1 answer

2


In your model, you have the following specification:

nome: { type: String }

But you are sending an array of objects:

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

Adjust your schema why nome contains an object instead of a string, or modify the payload on the client side before sending to the backend.

  • It worked! Didn’t know about Schematype "array". http://mongoosejs.com/docs/2.7.x/docs/schematypes.html Thank you!

  • 1

    @Fred I’m happy to read this, it’s always a pleasure to help. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.