this function lives in an index.js and is executed with node. The mysql queries are working perfectly fine but when I want to add a "count" to an object from the first result, i just get the following error message:
TypeError: Cannot set property 'count' of undefined
The Problem should be in
result[i].count = innerResult[0].count;
The array of objects looks like this:
[
RowDataPacket { name: 'Jane Doe', id: 1 },
...
]
And finally the code:
var msg = someInt;
con.query("SELECT `candidates`.`name`, `candidatures`.`id` FROM `elections` INNER JOIN `candidatures` ON `elections`.`position` = `candidatures`.`position` INNER JOIN `candidates` ON `candidatures`.`candidate` = `candidates`.`id` WHERE `elections`.`id` = '" + msg + "'", function (err, result, fields) {
if (err) throw err;
for(var i = 0; i < result.length ; i++){
con.query("SELECT COUNT(*) as `count` FROM `votes` WHERE `candidature` = '" + result[i].id + "'", function (innerErr, innerResult, innerFields) {
if (innerErr) throw innerErr;
result[i].count = innerResult[0].count;
});
}
Does the 2nd query overwrite the "result" even though the 2nd result is named different?