I'm using NodeJs, I'm creating a dynamic property inside an object, and then pushing it into an array.
The problem is, when I access the variable via console.log(object[dynamic_property_name])
I can see it in the console, but when I console.log(object)
the dynamic property doesn't show.
here is the code, the property in question is teachers_matieres
in the element object.
PS: I console logged the property (explicitly) in each step of the code, even just before the return, it shows. But when I try to print the object it doesn't show, neither in post man.
router.get('/get-all-messages', async function (req, res, next) {
let children = [];
const parent = await Parent.findOne({ _id: req.query.user_id });
const students = await Student.find({ parentPersonalId: parent.personalID });
for (let i = 0; i < students.length; i++) {
var element = students[i];
element['teachers_matieres'] = [0];
const inscription = await Inscription.findOne({ studentID: element._id });
const theClass = await Classe.findOne({ _id: inscription.classeID });
const schedule = await Schedule.findOne({ classeID: theClass._id });
for (let i = 0; i < schedule.teacherID.length; i++) {
if( schedule.teacherID[i]==null)
continue
const tuple = [
await Teacher.findOne({ _id: schedule.teacherID[i] }),
await Matiere.findOne({ _id: schedule.matierID[i] }),
];
// if (!tuple in element['teachers_matieres']){
console.log(element['teachers_matieres'].push(tuple));
// }
}
//director selection
const director = await Director.find({_id:inscription.directeurID})
element['director'] = director
const annexe = await Annex.find({_id:director.annexID})
const scholar = await ScholarGroup.find({_id:annexe.scholarGroupID})
const ceo = scholar.ceo
children.push(element);
}
const msgs = await Messagerie.find().or([
{ senderID: parent._id},
{ receiverID: parent._id ,
'approved.isApproved':true},
]);
return res.json({
error: false,
message: 'success',
data: { children: children, messages: msgs },
});
});
This is the output of postman:
{
"error": false,
"message": "success",
"data": {
"children": [
{
"localisation": {
"payer": "medea",
"wilaya": "medea",
"daira": "medea",
"commune": "medea"
},
"authentication": {
"code": "uhB9Y",
"expired": false
},
"tokens": [],
"unreadNotices": [],
"absence": [],
"inbox": [],
"msgSent": [],
"paymentTypes": [
"5f6a1749ddcfde4f3b0ebaf4"
],
"_id": "5f68a2ec489f029205bdaa42",
"firstNameFr": "abdou",
"lastNameFr": "moumen",
"firstNameAr": "moumen",
"lastNameAr": "بن احمدي",
"parentPersonalId": "dddd",
"birthday": "NaN-NaN-NaN",
"inscriptionNbr": "101",
"image": "",
"state": false,
"systemscolaire": "Interne",
"__v": 0
}
],
"messages": [
{
"approved": {
"isApproved": true,
"date": "21-9-2020",
"time": "17:22:4"
},
"fileUrl": [
{
"fieldname": "file",
"originalname": "cccc.pdf",
"encoding": "7bit",
"mimetype": "application/pdf",
"destination": "/home/admin/assets/schools/eco1/5f6899c86f84e38b88e270c0/messagesFiles",
"filename": "21-9-2020_cccc.pdf",
"path": "/home/admin/assets/schools/eco1/5f6899c86f84e38b88e270c0/messagesFiles/21-9-2020_cccc.pdf",
"size": 237233
}
],
"_id": "5f68dfae6a7b33b1c7b1f688",
"senderID": "5f68a354489f029205bdaa45",
"receiverID": "5f68a2d5cff2d591af390281",
"subject": "teacher ver parent",
"description": "<p>teacher ver parent</p>\n",
"date": "21-9-2020",
"time": "17:15:26",
"__v": 0
},
{
"approved": {
"isApproved": true,
"date": "22-9-2020",
"time": "10:53:39"
},
"fileUrl": [
{
"fieldname": "file",
"originalname": "NoticeSendReceive.js",
"encoding": "7bit",
"mimetype": "text/javascript",
"destination": "/home/admin/assets/schools/eco1/5f6899c86f84e38b88e270c0/messagesFiles",
"filename": "21-9-2020_NoticeSendReceive.js",
"path": "/home/admin/assets/schools/eco1/5f6899c86f84e38b88e270c0/messagesFiles/21-9-2020_NoticeSendReceive.js",
"size": 25393
}
],
"_id": "5f68e2174ce23cb39a7b9d22",
"senderID": "5f68a354489f029205bdaa45",
"receiverID": "5f68a2d5cff2d591af390281",
"subject": "teacher ver parent",
"description": "<p>message</p>\n",
"date": "21-9-2020",
"time": "17:25:43",
"__v": 0
}
]
}
}
I really need to append that property to each element, how can I do that? what's wrong with the dynamic property?