Encountering an issue when trying to update data in a Google sheet using Next.js. The error message ReferenceError: row is not defined keeps popping up and I can't figure out where I'm going wrong. Any help in resolving this error would be greatly appreciated. Feel free to ask any questions if needed, don't hesitate.
https://i.sstatic.net/Zm80A.png
sheet.js
This file contains my updated code.
export async function updateDataFromSheets() {
try {
const target = ['https://www.googleapis.com/auth/spreadsheets'];
const jwt = new google.auth.JWT(
process.env.GOOGLE_SHEETS_CLIENT_EMAIL,
null,
(process.env.GOOGLE_SHEETS_PRIVATE_KEY || '').replace(/\\n/g, '\n'),
target
);
const sheets = google.sheets({ version: 'v4', auth: jwt });
let values = [
[
'<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f6868497809f989d979a93cececfb6919b979f9ad895999b">[email protected]</a>',
'pravin kale',
'BE Comp',
'Success'
],
// Additional rows ...
];
const resource = {
values,
};
const response = await sheets.spreadsheets.values.update({
spreadsheetId: process.env.SPREADSHEET_ID,
range: 'sheet',
valueInputOption: row[6],
resource
}, (err, result) => {
if (err) {
// Handle error
console.log(err);
} else {
console.log('%d cells updated.', result.updatedCells);
}
});
} catch (err) {
console.log(err);
}
return [];
}
index.js
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import { getDataFromSheets } from '../libs/sheets';
export default function Home({ data }) {
return (
<div className={styles.container}>
<Head>
<title>Nextsheet 💩</title>
<meta
name="description"
content="Connecting NextJS with Google Spreadsheets as Database"
/>
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<h1>Welcome to Nextsheet 💩</h1>
<p>Connecting NextJS with Google Spreadsheets as Database</p>
<ul>
{data && data.length ? (
data.map((item) => (
<li key={item}>
{item.Email} - {item.Subject} - {item.Body} - {item.Status}
</li>
))
) : (
<li>Error: do not forget to setup your env variables 👇</li>
)}
</ul>
</main>
</div>
)
}
export async function getStaticProps() {
const sheet = await getDataFromSheets();
return {
props: {
data: JSON.parse(JSON.stringify(sheet.slice(0, sheet.length))), // remove sheet header
},
revalidate: 1, // In seconds
};
}
dynamicid.js
import { getDataFromSheets } from '../../../libs/sheets';
import sendmail from '../../../libs/ses/sendmail';
import { updateDataFromSheets } from '../../../libs/sheets';
export default function handler(req, res) {
var data;
getDataFromSheets()
.then(sheet => {
data = sheet.length
for (var i = 1; i < data; i++) {
if (sheet[i].Status != 'Success') {
sendmail(sheet[i].Email, sheet[i].Subject, sheet[i].Body)
}
}
res.json({ status: 'success', message: 'email has been sent' })
})
.catch(err => {
console.log(err)
res.json({ status: 'fail', error: err })
})
updateDataFromSheets();
}