'Knex Can't add the results of a query to an object
I am trying to make an object hbsObject to render for handlebars in express.
The object should contain a post and replies. I am using knex, express and postgresql.
The code:
app.get('/:id(\\d+)/', (req, res) => {
let hbsObject = {};
knex('post')
.where('id', '=', req.params.id)
.then(posts => {
if (posts.length > 0) {
hbsObject.post = posts;
console.log(hbsObject); "returns the post inside of hbsObject"
} else {
res.status(404).render('404');
}
});
console.log(hbsObject); //returns "{}"
knex('reply')
.where('original_id', '=', req.params.id)
.then(replies => {
if (replies.length > 0) {
hbsObject.replies = replies;
res.render('one', hbsObject);
} else {
res.status(404).render('404');
}
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
