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('Oper
Solution 1:
Refer to q.defer
example, please try to move return deferred.promise;
out of client.query
as below
functiongetElementInPostgres(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;
}
Solution 2:
You just have to return the promise outside client.query..
functiongetElementInPostgres(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.
Post a Comment for "Q.js Promise With Node. Missing Error Handler On `socket`. Typeerror: Cannot Call Method 'then' Of Undefined"