1

I am trying to get data from mysql with javascript and then do some things with each of the results. I have saved the mysql result to a variable and thought that I could iterate through this, but I cant understand how to do it.

To make it easy I have simplified the code as below, which uses forEach():

connection.query("SELECT length, weight FROM physics WHERE user = 'Test' AND name = 'Hi'", function (err, result) { 
      if (err) throw err;
      allres = result; 

allres.forEach(function(name) {
    console.log(`I looped: ${name}`);
  });
    });     

The result is:

I looped: [object Object]
I looped: [object Object]
I looped: [object Object]

If I do console.log(allres) I get:

RowDataPacket { length: '5', weight: '3' },
  RowDataPacket { length: '11', weight: '11' },
  RowDataPacket { length: '2', weight: '2' },

So the values are there, but how can I loop this?

2
  • 1
    When you do I looped: ${name}, toString() get's called on name, which for objects returns [object Object]. Instead, try accessing properties on the object, like I looped: ${name.length}, or convert the object to JSON: I looped: ${JSON.stringify(name)} Commented Sep 19, 2019 at 19:24
  • You are right, using properties did the trick Commented Sep 19, 2019 at 20:57

2 Answers 2

2

When you combine the object with a string it will console log everything as a string. You have a few options:

Console log the object by itself

connection.query("SELECT length, weight FROM physics WHERE user = 'Test' AND name = 'Hi'", function(err, result) {
    if (err) throw err;

    console.log('I have ' + result.length + ' results');

    result.forEach(function(entry) {
        console.log('I looped:'); // string
        console.log(entry); // object
    });
});

Console log each piece of each object

connection.query("SELECT length, weight FROM physics WHERE user = 'Test' AND name = 'Hi'", function(err, result) {
    if (err) throw err;

    console.log('I have ' + result.length + ' results');

    result.forEach(function(entry, index) {
        console.log('for entry with an index of : ' + index);
        console.log('the length value is : ' + entry.length);
        console.log('the weight value is : ' + entry.weight);
    });
});

Use the util module for nice tidy logging of nested objects

https://www.npmjs.com/package/util

var util = require('util')

connection.query("SELECT length, weight FROM physics WHERE user = 'Test' AND name = 'Hi'", function(err, result) {
    if (err) throw err;

    console.log(util.inspect(result, {showHidden: false, depth: null}));

});
Sign up to request clarification or add additional context in comments.

Comments

1
for (let i in name) {
    let item = name[i];
    // do something with item
}

Or you can access the values directly:

let length = name.length;
let weight = data.weight;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.