I am quite new to express and I created a database in Postgres to extract the data about blog posts to place the information in an ejs file. I get the error:
Cannot read property 'send' of undefined
I've tried to call db.getPosts()
with res
and req
, but it's not possible to set a header again, returns an error.
The problematic chunk of code in my query.js
file:
const getPosts = (_req, res) => {
pool.query('SELECT * FROM blog_posts', (error, results) => {
console.log(error);
// console.log(results.rows);
if (error) {
throw error
}
return res.send(results.rows );
})
}
send(results.rows)
or render('blog', {posts: results.rows})
called on res
give the exact same error.
Function in server.js
that is supposed to use this data is as follows:
app.get("/blog", function (req, res) {
const posts = db.getPosts();
res.render("blog", { posts: posts });
});
What do I do wrong? I lack some knowledge, that is for sure, so please, if you can help, explain this briefly to me if possible.
Also, is send()
function a correct function to get the data to operate on in server.js
? Many tutorials suggest json()
but then I don't really get the proper data format, it is just displayed in the browser.
Thank you very much.