0

In all tutorials I searched the web, they print the rows/result using console.log the from the mysql query. In my case, I want to store that value to manipulate it in a function. The problem is that node doesn't wait. Can you help me in catching the rows in such example? How can I store the rows in a variable?

con.connect(function (err) {
  if (err) throw err;
  con.query("SELECT * FROM customers", function (err, result) {
    if (err) throw err;
    console.log(result);
  });
});

I tested one of the solutions but I got [] when trying to get result/rows out of the function. I want to use the result in the savedServices: [] see below:
enter image description here

7
  • Use async module Commented Mar 11, 2018 at 12:43
  • i know it, but how to apply it, please make an answer to mark it. Commented Mar 11, 2018 at 12:44
  • Express not waiting doesn't have anything to do with express. It's related to node. Wrap this in a function and callback with results, then do whatever you need with the result, store it in a variable or whatnot. Commented Mar 11, 2018 at 22:45
  • Also, stop throwing errors and call back with the errors instead. If you don't, you'll have problems handling errors in your application. Commented Mar 11, 2018 at 22:46
  • @Diamond Let me know if you want me to write an example. Commented Mar 11, 2018 at 22:46

1 Answer 1

2

Give a fresh start with a simple project

Follow below commands

mkdir testdb
cd testdb
npm install --save mysql
npm install --save async

Create a test.js file

vim text.js

Now put below code in the test.js file

var mysql = require('mysql');
var async = require('async');

var con = mysql.createConnection({
  host: "localhost",
  database: "mydb",
  user: "user",
  password: "pass"
});

var myrows = [];

async.waterfall([
    function(callback) {
        con.connect(function(err) {
        if (err) throw err;
        console.log("Connected!");
        var sql = 'select * from users limit 5';
        con.query(sql, function (err, result) {
          if (err) throw err;

          result.forEach(function(item) {
            //console.log(item);
            myrows.push(item);
          });
          callback(null, myrows);
        });
      });
    }
],
// optional callback
function(err, myrows) {
    console.log("myrows:::");
    console.log(myrows);
});

now test the file

node test.js

You should see results like this:

Connected!
Result: [object Object],[object Object],[object Object],[object Object],[object Object]

Hope this helps!

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

8 Comments

I appreciate that, but you didn't store the results/rows in a variable, which I state clearly in the post!
thank you for interaction. I wanted to show the result to you on a screenshot, so I updated the post with an screenshot. please check it and let us keep going on it to solve it. thank you.
it is async issue.
Yes in this case you will need to use async. Have a look at this stackoverflow.com/a/30973427/1225070
i am tired of applying that and couldn't achieve it, if that easy for you, please update your answer with the fix. I want to see how applied. I want to mark your answer as best answer.
|

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.