1

I am implementing an API to manage a MySQL database from Nodejs, and I want to keep my code clean as the work I intend to do can take me to have a lot of nested connection.query functions all over the place, so I've decided to separate as many as the queries I could into separate functions.

To start I wanted to do a function to check if there is an existing element already on the table, like this:

function validator(item) {
    return new Promise(function(err,done) {
    connection.query('select * from items where item_id = ?', [item.id] , function(err,rows){
        console.log(rows);
        console.log("above row object");
        if (err) {
            return done(false);
        }
        if (rows.length) {
            console.log('That item is already in the DB.');
            return done(false);
        } else {
            console.log('That item is new.');
           return done(true);
        } 
    });
});
}

And then I want to use it like this:

    var insert = function (item, done) {
    validator(item).then(function(err,done) {
        console.log('here I go: '+done);
        if(done) {
            console.log('good');
        } else{
            console.log('bad')
        }
      );
    }

But it does not work like I expected, the first function accurately detects if an item exists or not, but the second one never never reaches the "here I go" print.

I have checked Use promise to process MySQL return value in node.js, Javascript Promise node.js? and Javascript & promises with Q - closure problems in promises but the conclussion I reach is that this is a classic PEBCAK problem, such as that I'm a very incompetent javascript programmer and can't wrap my head around the callbacks and promises all that well so I'm managing them the wrong way.

Community
  • 1
  • 1
Daniel Ortiz
  • 21
  • 1
  • 2

1 Answers1

1

The first callback you pass to the Promise constructor isn't the failing one.

Change

return new Promise(function(err,done) {

into

return new Promise(function(done, reject) {

and call it like this:

validator(item).then(function(done) {

(by the way it's very confusing to call done both the resolve callback and the result).

Handle the incoming error: Change

if (err) {
        return done(false);
}

into

if (err) {
        reject(err);
}

and don't forget to catch those errors.

Also, it's probably outside the scope of this QA but you usually take and release connections. You should probably have a look at promises ready mysql drivers.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • This seems to have helped me, but now my print console.log('here I go: '+done); always tells me "here I go undefined", shouldn't 'done' be 'true' as the return on the callback when an item does not exist? Also yeah I know I need to release the connections, this is just a small test of concept. – Daniel Ortiz Feb 01 '17 at 16:39
  • @DanielOrtiz I edited to point a few other problems. And I suggest renaming that "done". – Denys Séguret Feb 01 '17 at 16:48
  • Thank you a lot, your pointers helped me solve the problem, in fact the last part was a product of me being tired as I was using validator(item).then(function(err,done) { instead of validator(item).then(function(done, err) { and as such the 'done' was undefined. – Daniel Ortiz Feb 01 '17 at 18:33