I need to save some sub documents in a schema which is sub document of a schema. The save function is this:
exports.add = function(req, res){
var cliente = new Cliente(req.body);
var sedi = [];
for(var key in req.body.sede){
var sede = new Sede(req.body.sede[key]);
var luoghi_consegna_sedi = [];
for(jey in req.body.sede[key].lcs){
var luogo_consegna_sede = new LuoghiConsegnaSede(req.body.sede[key].lcs[jey]);
//Sub document
luoghi_consegna_sedi.push(luogo_consegna_sede);
}
sede.luoghi_consegna_sedi = luoghi_consegna_sedi;
//Sub docuemnt
sedi.push(sede);
}
cliente.sedi = sedi;
cliente.save(function(err){
if(err)
return res.sendStatus(400);
return res.json(cliente);
});
};
The problem is that the top parent schema (cliente) is saved on mongoldb, while the two types of sub documents don't. In cliente.sedi the array is filled with sede objectid, but on mongoldb the sede table doesn't exists (and the same for luoghi_consegna_sedi). If I manually save sede and luoghi_consegna_sedi before push them in their arrays, the two tables are creates and filled with data, but if I run populate() on cliente.sedi I get an empty array. The 3 Schemas are these: Cliente
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var ClienteSchema = new Schema({
data_status: String,
status_cliente: Number,
titolo_cliente: String,
cognome: String,
nome: String,
ragione_sociale: String,
codice_fiscale: String,
partita_iva: String,
data_nascita: String,
luogo_nascita: String,
business: {
type: Boolean,
default: false
},
consumer: {
type: Boolean,
default: false
},
sedi: {
type: [Schema.ObjectId],
ref: 'Sede'
},
eliminato: {
type: Boolean,
default: false
},
created: {
type: Date,
default: Date.now
}
});
ClienteSchema.set('toJSON', {getters: true});
mongoose.model('Cliente', ClienteSchema);
Sede
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var SedeSchema = new Schema({
denominazione_sede: String,
tipo_sede: String,
tipo_indirizzo: String,
indirizzo: String,
numero_civico: String,
citofonare: String,
interno: String,
piano: String,
scala: String,
citta: String,
provincia: String,
cap: String,
regione: String,
paese: String,
telefono_1: String,
telefono_2: String,
email: String,
agente_assegnato_1: {
type: Schema.ObjectId,
ref: 'User'
},
agente_assegnato_2: {
type: Schema.ObjectId,
ref: 'User'
},
agente_assegnato_3: {
type: Schema.ObjectId,
ref: 'User'
},
agente_assegnato_4: {
type: Schema.ObjectId,
ref: 'User'
},
agente_assegnato_5: {
type: Schema.ObjectId,
ref: 'User'
},
agente_assegnato_jolly: {
type: Schema.ObjectId,
ref: 'User'
},
titolo_rif: String,
cognome_rif: String,
nome_rif: String,
cellulare_rif: String,
email_rif: String,
telefono_rif: String,
luoghi_consegna_sedi:{
type: [Schema.ObjectId],
ref: 'LuoghiConsegnaSede'
},
eliminato: {
type: Boolean,
default: false
},
created: {
type: Date,
default: Date.now
}
});
SedeSchema.set('toJSON', {getters: true});
mongoose.model('Sede', SedeSchema);
Luoghi_consegna_sedi
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var LuoghiConsegnaSedeSchema = new Schema({
titolo_rif: String,
cognome: String,
nome: String,
tipo_sede_consegna: String,
sede_consegna: String,
telefono_1: String,
telefono_2: String,
cellulare_rif: String,
email_rif: String,
telefono_rif: String,
tipo_indirizzo_consegna: String,
indirizzo_consegna: String,
n_civico_consegna: String,
piano_consegna: String,
interno_consegna: String,
scala_consegna: String,
citofonare_consegna: String,
citta_consegna: String,
provincia_consegna: String,
regione_consegna: String,
cap_consegna: String,
paese_consegna: String,
eliminato: {
type: Boolean,
default: false
},
created: {
type: Date,
default: Date.now
}
});
LuoghiConsegnaSedeSchema.set('toJSON', {getters: true});
mongoose.model('LuoghiConsegnaSede', LuoghiConsegnaSedeSchema);