pk36
pk36

Reputation: 33

Create an object with mysql query

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.

Upvotes: 0

Views: 569

Answers (1)

wlh
wlh

Reputation: 3515

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

Upvotes: 1

Related Questions