Reputation: 327
I've been working with mongoDB in node using mongoose. The following query to mongoose is made when a GET request is made to a route (e.g. localhost:7000/restaurants) and displays an array full of matching objects
async func() {
return this.Restaurants.find({$or: [{city: somecity}, {postal_code: somepostalcode}]}).then(result => {
return result;
})
}
I tried a similar thing using postgres, however, calling it returns nothing on the browser, but is successfully logged onto the console:
async func(){
return await client.query(`SELECT * FROM restaurants WHERE city = '${somecity}' OR postal_code = '${somepostalcode}';`, (err, res) => {
console.log(res.rows);
return res.rows;
})
}
Why does this happen, if they both seem to return the same type of object? Is there a way to make the postgres result visible?
Thank you for the help!
Upvotes: 0
Views: 26
Reputation: 3316
It is most probably because postgres API is callback based. You would need to wrap callback in promise and return query response in resolve. Updating code to something like below should help:
async func(){
return new Promise((resolve, reject) => {
client.query(`SELECT * FROM restaurants WHERE city = '${somecity}' OR postal_code = '${somepostalcode}';`, (err, res) => {
if(err) {
return reject(err)
}
return resolve(res.rows);
})
})
}
Upvotes: 1