1

The problem is so basic: I want to save nested objects with mongoose in mongodb, but when I save them, nested objects are empty

I have tried other responses to this problem but that dont fix my problem. Pushing object, populating etc.. but that only work when you require that info not to save it. So here I am.

Model


const mongoose = require('mongoose')
let Schema = mongoose.Schema
const respuestaSchema = new Schema({
    resp:{type:String},
    value:{type:String}
},{ _id : false })

const preguntasSchema = new Schema({
    pregunta:{type:String},
    tipo:{type:String},
    respuesta:[respuestaSchema]
},{ _id : false })

let testSchema = Schema({
    _id:{type:Schema.ObjectId, auto:true},
    name: {type:String},
    dName:{type:String},
    categoria:{type:[Schema.ObjectId],ref:'categoria'},
    preguntas:[preguntasSchema]

},{versionKey:false})

module.exports = mongoose.model('test',testSchema)

Saving test:

const Test = require('../models/test')

exports.newTest = function(req,res){
    let param = req.body
    let nTest = new Test()
    nTest.name = param.name
    nTest.dName = param.dName
    nTest.categoria = param.categoria
    nTest.pregunta = param.pregunta
    nTest.tipo = param.tipo
    nTest.resp = param.resp
    nTest.value = param.value
    nTest.preguntas.push(nTest)
    nTest.save().then(
        testSaved=>{
            res.status(200).send({accion:'newTest',mensaje:'Test creado correctamente'})
        }
    ).catch(err=>{res.status(500).send({accion:'newTest',mensaje:'Error en creacion de test ' +err})})
}

I do nTest.preguntas.push(nTest) to save preguntas and it saved it, but the info into that object dont appear, only appear object[] because preguntas have another object inside.

I expected to see at Compass or Robo3T all the info I pass, but I only see the info that is not a nested object, and the title of nested objects but empty. If you need a img capture of the problem in db i can send. Hope you can help me with this info.

1
  • @romedu check IIarion Halushka answer, that work for me. Commented May 22, 2020 at 7:16

1 Answer 1

1

I edited your code corresponding to the schemes you have declared, please try:

exports.newTest = ({ body: param }, res) => {
  const nTest = new Test();
  nTest.name = param.name;
  nTest.dName = param.dName;
  nTest.categoria = param.categoria;
  nTest.preguntas = [
    {
      pregunta: param.pregunta,
      tipo: param.tipo,
      respuesta: [
        {
          resp: param.resp,
          value: param.value,
        },
      ],
    },
  ];
  // your saving func
};
Sign up to request clarification or add additional context in comments.

2 Comments

It works, thank you so much. I cant vote up the answer coz Im new in stackOverflow but this answer work to me.
"To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in." stackoverflow.com/help/someone-answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.