I am currently facing a challenge while using the Google Sheets API with Express, as I have limited experience with JavaScript. My goal is to pass a JSON object from Express to React, but for some reason, when I send the object, it appears empty on the frontend.
I have attempted to use res.body/res.data without success. Additionally, I have made sure to include as many awaits as possible to ensure that the object is fully loaded before sending it. However, nothing seems to resolve the issue. When trying to use res.json or res.send with just the response object, I encounter a circular structure conversion error to JSON. Below is the code snippet I am working with:
async function docShit() {
// Initialize the sheet - doc ID is the lengthy ID in the sheets URL
const doc = new GoogleSpreadsheet(
" --SPREADSHEET ID-- "
);
// Initialize Auth - view https://theoephraim.github.io/node-google-spreadsheet/#/getting-started/authentication
await doc.useServiceAccountAuth({
client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
private_key: process.env.GOOGLE_PRIVATE_KEY,
});
await doc.loadInfo();
const sheet = doc.sheetsByTitle[--WORKSHEET TITLE--];
const rows = await sheet.getRows();
return rows;
}
app.get("/home", async (req, res) => {
try {
await docShit()
.then((response) => {
res.send(Promise.resolve(response));
})
.catch((err) => console.log(err));
} catch (err) {
console.error(err.message);
}
});