Wednesday, 24 May 2017

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.



via pk36

No comments:

Post a Comment