TypeError: Cannot read properties of null (reading 'idTechnical')

Viewed 94

I was trying from some object get others info from it. Like from object A, get the objectId from object B, and printing the object B instead the object A. But I got this error message: TypeError: Cannot read properties of null (reading 'idTechnical') I'm using nodejs

My function:

const getFinalReport = async (req, res) => {
try{
    const finalReport = await Final.findById(req.params.id);
    const technicalReport = await Technical.findById(finalReport.idTechnical);
    const finantialReport = await Finantial.findById(finalReport.idFinantial);
    const writeReport = await Write.findById(finalReport.idWrite);
    
    res.status(200).json(finalReport, {technicalReport, finantialReport, writeReport});
}catch(err){
    console.error(err);
    res.status(500).send({"msg": "Internal Error!"})
}}

My model:

const finalreportSchema = new mongoose.Schema({
    idWrite: { type: mongoose.Schema.Types.ObjectId, ref: "reports", default:''},
    idTechnical: { type: mongoose.Schema.Types.ObjectId, ref: "technincal", default: ''},
    idFinantial: { type: mongoose.Schema.Types.ObjectId, ref: "finantial", default:''},
    isConclude: { type: Boolean, default: false }
},
{
    timestamps:true
});
const Report = mongoose.model('final-report', finalreportSchema);
module.exports = Report;

My techincal model:

const mongoose = require("mongoose");

const TechincalSchema = mongoose.Schema({
    idInstallationNumber: { type: mongoose.Schema.Types.ObjectId, ref: "installations" },
    months: {  type: String, required: true },
    previousBalance: { type: String, required: true },
    actualBalance: { type: String, required: true },
    injected: { type: String, required: true },
    totalInjected: { type: String, required: true },
},
{
    timestamps:true
});

const Technical = mongoose.model('technincal', TechincalSchema);

module.exports = Technical;

Same happen on the Finantial and Write Reports. Had someone had some clue?

Sorry for my english but, this is the first time asking, and also it's been a while that I write something in english (isn't my first language)

0 Answers
Related