简体   繁体   中英

Q.js promise with node. Missing error handler on `socket`. TypeError: Cannot call method 'then' of undefined

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 !

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM