I can see an error and something worrying me.
The error is in following two lines
// In controller.js
model.getData( function (err, objects) {
// In models.js
const getData= (request, response) => {
You define a function accepting two arguments (request, response) (both of them are objects) and than you call it passing only one argument of type function.
The thing worrying me is the getData function itself.
const bl = {"hello":"world"}
const getData= (request, response) => {
return(bl);
}
Apart from parameters, it is a sync function, but in your question you pointed that in models.js you query in the Postgres database, and you can see the results in console.log; now you give us no details about how you query Postgress, but I suppose you are doing it with something asynchronous (probably pg). The other relevant detail you didn't revealed is if you query Postgress inside or outside the body of getData function. I hope I'm wrong, but since in your example you are returning something define outside the body of the function, I have the doubt.
Let's consider only the good option, you query Postgress inside the body of the function, probably your complete getData function looks like follows.
const getData = (request, response) => {
client.query('SELECT $1::text as message', ['Hello world!'], (err, res) => {
// Here you get your message in console
console.log(err ? err.stack : res.rows[0].message);
client.end();
})
return something;
// I don't know what, but fore sure something wrong
}
To solve both the problems you just need to fix getData (by chance or by cut&paste you are calling it in the right way in controller.js).
Let's start fixing its signature makeing it accept only one argument: the callback function:
const getData = (done) => {
then let's asynchronously "return" the data we got from Postgress through the done callback function.
const getData = (done) => {
client.query('SELECT $1::text as message', ['Hello world!'], (err, res) => {
// We can leave this for debugging purposes
console.log(err ? err.stack : res.rows[0].message);
// Here we are not handling errors from client.end()...
// but we can neglect about this right now
client.end();
// Let's calle the callback function passing it the result
done(err, res);
})
// There's nothing to return
}
It should be enough.
Hope this helps.