Saturday, 8 April 2017

Node.js + mysql : no connection on UPDATE query

I'm trying to generate fake names on a MySQL database table for a project. So when I do a select query on the table in order to know the number of rows on which to do my loop of fake names generation, everything works fine. But then when I'm trying to do the actual update query, I don't know why it doesn't work. I do have the loop working as my console.log before and after the query are appearing, but it looks like it doesn't even try to connect to the database when querying an update.

Here after are my client.js and index.js ->

client.js

'use strict';

/**
 * Module dependencies.
 */

const mysql = require('mysql');
const debug = require('debug')('db');

/**
 * Export the MySQL client pre-handler.
 */

module.exports = {
  assign: 'client',
  method: (request, reply) => {
    debug('Connecting the database')
    const client = mysql.createConnection({
      user: 'root',
      password: 'root',
      socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock', // Replaces host and port
      database: 'db'
    });
    client.connect(function(err) {
      if (err) {
        console.error('Error Connecting: ' + err.stack);
        return;
      }
      console.log('Connected as ID ' + client.threadId);
    });

    return reply(client);
  }
};

index.js

'use strict';

/**
 * Module dependencies.
 */

const os = require('os');
const joi = require('joi');
const debug = require('debug')('server');
const faker = require('faker');

/**
 * New Hapi server
 * (HTTP connection).
 */

debug('New HTTP server');
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({
  host: 'localhost',
  port: 1337,
  routes: {
    cors: true
  }
});

/**
 * Fakes data.
 */

server.route({
  method: 'PUT',
  path: '/faker',
  config: {
    validate: {

    },
    pre: [
      require('./client')
    ]
  },
  handler: (request, response) => {
    let nb_rows;
    let firstname;
    let lastname;
    request.pre.client.query('SELECT COUNT(*) as rows FROM clients', function (error, results, fields) {
      if (error) throw error;
      let nb_rows = results[0].rows;
      for(let i = 1; i <= nb_rows ;i++) {
        let firstname = faker.name.firstName();
        let lastname = faker.name.lastName();
        console.log(firstname);
        request.pre.client.query('UPDATE clients SET firstname = ?, lastname = ? WHERE ID = ?', [firstname, lastname, i], function (error, results, fields) {
          if (error) throw error;
          console.log("HHHEEEEEEEEEEEEEEEYYYYYYYYYY");
        });
        console.log(i);
      };
    });
  }
});

/**
 * Start the server.
 */

debug('Start the HTTP server');
server.start(err => {
  if (err) {
    throw new Error(err)
  }

  console.log(`Server running at ${server.info.uri}`);
});

I've spend a lot of time looking for answers, and I've seen a lot of issued posts on node.js / mysql update problems. But I haven't find any matching mine, or maybe I didn't understood it.

If you have any ideas, that would be much appreciated.

Thanks.



via Antoine Langlet

No comments:

Post a Comment