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.