I created a separate file with all my queries using pg-promise
module for Node. While for most of them I just use req, res
after a query, for one I want to return a value back. It does not work. It returns undefined
.
passportLogin: (email)=> {
db.one(`SELECT userid
FROM user`)
.then( result => {
return result;
})
.catch( error => {
return error;
});
}
That code has two problems at the same time:
.catch
you do return result
, that's not how promise rejects are handled, you must either provide the error handling, or re-throw / re-reject the error.I need to catch if it returns more than one row...
, which is a logical contradiction.The result of it is as follows: your query executes successfully, and returns more than one record, which in turn makes method one reject, and then you take the rejection reason and turn it into a resolved one by doing return result
. In all, your code is broken all over the place.
First, with pg-promise you are supposed to use the right method, according to the number of records you are expecting back, see The Basics.
And then handle .then/.catch
according to your business logic. I can't be more specific here, because you did not provide further details on this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With