I'm using moongose to create two models that need to be 'connected'. 'Paciente' and 'Consulta'. One 'Paciente' can have many 'Consultas'... so I created my models and I wanted to test them using a create endpoint, but for some reason I'm getting an error because of the way I'm trying to push new 'Consultas' into Paciente
Paciente model code:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const pacienteSchema = new Schema({
guardianName: {
type: String,
required: true
},
mascotaName: {
type: String,
required: true
},
direccion: {
type: String,
required: true
},
cedula: {
type: String,
required: true
},
tel: {
type: String,
required: true
},
email: {
type: String
},
esVoluntario: {
type: String,
required: true
},
numChip: {
type: String,
required: true
},
especie: {
type: String,
required: true
},
raza: {
type: String,
required: true
},
sex: {
type: String,
required: true
},
age: {
type: String,
required: true
},
peso: {
type: String
},
vacunado: {
type: String,
required: true
},
desparacitado: {
type: String,
required: true
},
consultas: [
{
type: Schema.Types.ObjectId,
ref: "Consulta"
}
]
});
module.exports = mongoose.model("Paciente", pacienteSchema);
Consulta model code:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const consultaSchema = new Schema({
pacienteId: {
type: Schema.Types.ObjectId,
ref: "Paciente"
},
motivo: {
type: String,
required: true
},
pruebas: [{ type: String }],
Observaciones: {
type: String
},
diagnostico: {
type: String
},
tratamiento: {
type: String
}
});
module.exports = mongoose.model("Consulta", consultaSchema);
create consulta endpoint:
const Consulta = require("../models/consulta");
const Paciente = require("../models/paciente");
exports.createCon = async (req, res, next) => {
const consulta = new Consulta({
pacienteId: req.body.pacienteId,
motivo: req.body.motivo,
pruebas: req.body.pruebas,
observaciones: req.body.observaciones,
diagnostico: req.body.diagnostico,
tratamiento: req.body.tratamiento
});
try {
// const consulta = new Consulta(req.body);
await consulta.save();
const paciente = await Paciente.findById(req.pacienteId);
paciente.consultas.push(consulta);
await paciente.save();
res.status(201).json({
message: "¡Consulta creada con éxito!",
consulta: consulta
});
} catch (err) {
if (!err.statusCode) {
err.statusCode = 500;
}
next(err);
}
};
I'm using postman to try the endpoint in this way:
{
"pacienteId": "5d961758e627d543445925ce",
"motivo": "Picazon en la colita, se rasca y lame mucho",
"pruebas": ["Pruebas Rápida", "Cropológico"],
"diagnostico": "pulgas y falta de vitaminas",
"tratamiento": "shampoo y spray antipulgas, pipeta 120ml, pipeta vitaminica 50 ml"
}
pacienteId
is an id that already exits in my paciente collection
And this is the error message I'm getting:
TypeError: Cannot read property 'consultas' of null
Visual Studio code points me the error on the line where I push consulta into Paciente
As far as I can see, the issue is on the line where you create the paciente constant,
const paciente = await Paciente.findById(req.pacienteId);
The pacienteId is located inside the req.body variable, and not the req variable, so the line should be,
const paciente = await Paciente.findById(req.body.pacienteId);
As req.pacienteId does not have the patient Id, paciente ends up as null because the function returns nothing. That's why you get an error. Please try this and see if it works.