Q.js promise with node. Missing error handler on `

2019-07-25 09:48发布

问题:

I´m rookie with nodejs and with promises, I installed Q.js with npm :

npm install q

I´m try yo make a promise when I made a query to postgres, this is my code...

socket.on('Operation', function (data) {
....... 
getElementInPostgres(makeQuery)
    .then(function (name) {
          console.log("promiseeee then");
          .......blablabla
      })
    .fail(function (err) {
      console.log("promiseeee error");
    });
.............

function getElementInPostgres(makeQuery){
  console.log("entro getElementInPostgres");
  var deferred = Q.defer();
  client.query(
                  makeQuery,
                  function(err, result) {
                    if (err) {
                       console.log("NO getElementInPostgres");
                      console.log(err);
                      deferred.reject(err);
                    } else {
                       console.log("ok getElementInPostgres");
                     console.log(result);
                     deferred.resolve(result);
                    }
                    return deferred.promise;
                });
}

But my code crash

entro getElementInPostgres
Missing error handler on `socket`.
TypeError: Cannot call method 'then' of undefined
    at Socket.<anonymous> (/var/www/test.smartparking/nodejs/server.js:247:6)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Socket.onevent (/var/www/test.smartparking/nodejs/node_modules/socket.io/lib/socket.js:335:8)
    at Socket.onpacket (/var/www/test.smartparking/nodejs/node_modules/socket.io/lib/socket.js:295:12)
    at Client.ondecoded (/var/www/test.smartparking/nodejs/node_modules/socket.io/lib/client.js:193:14)
    at Decoder.Emitter.emit (/var/www/test.smartparking/nodejs/node_modules/socket.io/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20)
    at Decoder.add (/var/www/test.smartparking/nodejs/node_modules/socket.io/node_modules/socket.io-parser/index.js:247:12)
    at Client.ondata (/var/www/test.smartparking/nodejs/node_modules/socket.io/lib/client.js:175:18)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Socket.onPacket (/var/www/test.smartparking/nodejs/node_modules/socket.io/node_modules/engine.io/lib/socket.js:101:14)

I was looking at examples and do not see anything different in my code ...

Thanks !

回答1:

Refer to q.defer example, please try to move return deferred.promise; out of client.query as below

function getElementInPostgres(makeQuery){
  console.log("entro getElementInPostgres");
  var deferred = Q.defer();
  client.query( makeQuery,
                  function(err, result) {
                    if (err) {
                       console.log("NO getElementInPostgres");
                      console.log(err);
                      deferred.reject(err);
                    } else {
                       console.log("ok getElementInPostgres");
                     console.log(result);
                     deferred.resolve(result);
                    }
                });
     return deferred.promise;
}


回答2:

You just have to return the promise outside client.query..

function getElementInPostgres(makeQuery){
    console.log("entro getElementInPostgres");
    var deferred = Q.defer();
    client.query(makeQuery, function(err, result) {
        if (err) {
            console.log("NO getElementInPostgres");                               
            console.log(err);
            deferred.reject(err);
        } else {
            console.log("ok getElementInPostgres");
            console.log(result);
            deferred.resolve(result);
        }
    });
    return deferred.promise;
}

client.query is an async method....so when you call

getElementInProgress(makeQuery).then(...);

the .then() will be called before client.query returns.