Saturday 27 May 2017

Nodejs loop through mysql Query give result = undefined

i have a Json Object and want to loop thorugh it. In my loop I want to execute a select query and save it result to another json Array.

So I start with a loop, performe a mysql query and want just log the results:

for (var x = 0; x < obj.length; x++) {
     var query = connection.query('SELECT Id, date FROM tbl1 WHERE Id LIKE '+ mysql.escape(obj[x].ident) +'; SELECT Id, date FROM tbl WHERE Id LIKE '+ mysql.escape(obj[x].ident) +' ORDER BY date DESC Limit 1 offset 1', function (error, results, fields) {
            if (error) {throw error;}
            if (!error) {
                console.log("resultset"+results); //<- Here I get the right result
                console.log("ID: "+results[0].Id); //<- UNDEFINED
                console.log("ID-LAST-entry: "+ results[1].Id); //<- UNDEFINED
                          } });

The Problem is that for booth results (0 and 1) i get = UNDEFINED. (When I log results i get the right result set.

Where is my mistake?



via Ckappo

No comments:

Post a Comment