I have a function in a backend code file that successfully retrieves and returns data from an external database:
export async function getData() {
/*
DB authorization code here
*/
let result = await db.query.execute({
sql: `select * from ${'dataset name'}.${'table name'},
params: { id: 2 }
})
result = JSON.stringify(result, null, ' ');
return result;
}
I then call the function getData() in my frontend page code to get the string of data, and then output the response to the console:
import { getData } from 'backend/data';
$w.onReady(function () {
getData().then(response => {
console.log("Response: " + response);
})
})
The output to the console is, as expected, a string of data (simplified for example’s sake):
Response:
[
{
"name": "John",
"id": "1",
"value": "100",
},
{
"name": "Tim",
"id": "2",
"value": "200",
},
{
"name": "Jim",
"id": "3",
"value": "300",
}
]
However, when using JSON.parse on this string, I’m getting an unexpected result:
import { getData } from 'backend/data';
$w.onReady(function () {
getData().then(response => {
let response_obj = JSON.parse(response);
console.log("Response obj: " + response_obj);
})
})
This is what logs to the console:
Response obj: [object Object],[object Object],[object Object]
What is going wrong in the code above where I’m getting “[Object object]” for each item in the database, rather than a Javascript object containing all of the data shown above?
Thank you in advance for your help!