Reputation: 1671
I'm using NodeJS and need to fetch an audioid
field from my first MySQL query, assign it to a variable and use that variable in my next MySQL query. So I've tried to use;
var newaudioid = results[0].audioid;
But the result is coming back as "undefined".
Here's the code;
connection.query(sql, [1, 2], function(err, results, fields){
if (!err) {
res.send(JSON.stringify(results[0]) + JSON.stringify(results[1]));
console.log(results[0]);
console.log(results[1]);
var newaudioid = results[0].audioid;
console.log('newaudioid = ' + newaudioid);
} else {
console.log('Error while performing query.');
console.log(err);
}
});
So console.log('newaudioid = ' + newaudioid);
gives me back newaudioid = undefined
How can I fetch that audioid
field from my first query?
Upvotes: 0
Views: 160
Reputation: 2884
Following up on your comment
SO I stringified IE: console.log('results0 = ' + JSON.stringify(results[0])); and got results0 = [{"audioid":144},{"audioid":147}]
Inside results[0] you have 2 objects that themselves have another object inside, and they both have the same property name. The problem is that javascript does not know which one you are referring to when you are saying results[0].audioid
What you can do is use [0] again to get the first one again, so that would be: results[0][0]
Upvotes: 1