0

i'm trying to access the property value of a query, however it says it can not read the det_id property, it follows the code:

    var detname_img = [];
    var sqlEdit = "INSERT INTO images_det SET ?";

    for(var k=0; k < object.det_img.length; k++){

        detname_img.push({
            name_img : object.det_img[k].name_img,
        });

        connection.query(sqlEdit,detname_img[k]);
    }

    console.log(detname_img);

    for(var i = 0; i < detname_img.length; i++){

        var getDet = ("SELECT det_id from images_det where name_img = '" + detname_img[i].name_img + " ' order by 1 desc limit 1");

        connection.query(getDet, function(erro,result){
            console.log(result[i].det_id);
        });

    }

My console.log(result):

[ RowDataPacket { det_id: 155 } ]
[ RowDataPacket { det_id: 156 } ]
[ RowDataPacket { det_id: 157 } ]
[ RowDataPacket { det_id: 158 } ]
  • if there is that error, then how are the values getting printed in your console log statement?? – sanatsathyan Jan 27 '18 at 15:06
  • good question, i dont know why, but when i console.log(result) i get all values, but when i try to console.log(result[i].det_id) return 'Cannot read property 'det_id' of undefined' – Caio Pereira Jan 27 '18 at 15:12
  • i'm sorry, but since your connection.query is asynchronous, it is returning undefined – sanatsathyan Jan 27 '18 at 15:13
  • i'm using promise, this change something? – Caio Pereira Jan 27 '18 at 15:14
  • Did you see that, result is an array, and your console ouput confirms it. So maybe you should use result[0].det_id? – Kinka Jan 27 '18 at 15:19
  • Yeah! This give me a light, I'll try to use this and come back here to tell you if it worked, thank you a lot – Caio Pereira Jan 27 '18 at 15:27
  • It did not work, but I was able to group the id's into an object, but I can not return it to the next .then () of the promise – Caio Pereira Jan 28 '18 at 04:53
  • Possible duplicate of [I can't return a object in .then() Promise](https://stackoverflow.com/questions/48483312/i-cant-return-a-object-in-then-promise) – HMR Jan 29 '18 at 03:54

0 Answers0