I am trying to retrieve data from a PSQL table. And this is my code for that:
async function requestData() {
var selectQuery = `SELECT "fName", "lName", "phoneNumber", "eMail" FROM public."User"`;
const {rows} = await client.query(selectQuery);
client.end;
return rows;
}
This function is returning: an array inside the Promise keyword. {[]}
I want to destructure the array inside to present the data and I cannot figure out any possible way of doing it. I tried using foreach loop and other ways but it is not working.
When this function runs in its own file, it gives normal output of an array of objects.
async function requestData() {
var selectQuery = `SELECT "fName", "lName", "phoneNumber", "eMail" FROM public."User"`;
const {rows} = await client.query(selectQuery);
client.end;
console.log(rows);
return rows;
}
This is the code in the file that I have imported:
const adminData = require("./data/adminDataObjects");
console.log(adminData.users.table);
And this is the code in the adminDataObjects file where I have defined this function:
users: {
body: ``,
table: requestData(),
script: `<script>document.querySelector('.users').classList.add('active')</script>`,
},
I am new to node js and I have already tried to search for the solution in different blog posts, stackoverflow answers and MDN documentation. I even read blog posts about asynchronous javascript and completed a tutorial on promises.