0

I can console.log the object that I want, but how can I retrieve the object outside of connection.query()?

var express = require('express');
var mysql = require("mysql");

var app = express();

var connection = mysql.createPool({
  connectionLimit: 50,
  host: "localhost",
  user: "root",
  password: "",
  database: "sakila"
});

app.get('/', function(req, res){
  connection.query('SELECT * FROM actor', function(err, rows) {
    if(err) throw err;

    var user = rows[0];
    console.log(user);

  });

  res.send();
});

This is the result in the console:

node-databases> node .\database\db-1.js
RowDataPacket {
  actor_id: 1,
  first_name: 'PENELOPE',
  last_name: 'GUINESS',
  last_update: 2006-02-15T10:34:33.000Z }

I want to be able to take that and send it in the response to the browser.

2
  • Replace console.log with res.send thus sending response within the callback Commented May 25, 2017 at 3:51
  • 1
    You're a genius! I actually figured this out while trying Knex, and when you told me about that it all made sense. Thank you!!! Write that as an answer and I'll give you some internet points. Commented May 25, 2017 at 4:05

1 Answer 1

1

Replace console.log with res.send thus sending response within the callback.

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

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.