Q.js承诺节点。缺少`socket`上的错误处理程序。 TypeError:无法调用未定义

时间:2016-02-26 07:59:05

标签: javascript node.js promise

我是nodejs和promises的新手,我用npm安装了Q.js

npm install q

当我向postgres发出查询时,我会尝试做出承诺,这是我的代码...

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

但我的代码崩溃了

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)

我正在查看示例,并且在我的代码中看不到任何不同......

谢谢!

2 个答案:

答案 0 :(得分:1)

请参阅q.defer example,请尝试将return deferred.promise;移出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;
}

答案 1 :(得分:1)

您只需要在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是一种异步方法....所以当你调用

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

在client.query返回之前将调用.then()。