Why am I Receiving "Cannot Read Properties of Undefined (reading 'values')" Error in My Node Code to Search a Key in Google Sheets?

This topic was automatically generated from Slack. You can find the original thread here.

Hi . i have wrote a node code to search for a key in google sheets spreadsheet, and return a value that matches the key. I have used the credentials for signung google drive, from previous step in pipedream, however i get the “Cannot read properties of undefined (reading ‘values’)”. what can be the reson?

For the step that’s retrieving the spreadsheet, are you sure the step is returning the spreadsheet data? Have you taken a look at the Exports tab of that step to make sure it’s pulling the data you expect?

this is my code: import { axios } from@pipedream/platform”

export default defineComponent({
props: {
google_sheets: {
type: “app”,
app: “google_sheets”,
}
},
async run({ steps, $ }) {
const spreadsheetId = “1T2qcjzKm0NvptN_AF2vfRSH9PSMs7lGgUQCcASfhbxY”; // Replace with your actual spreadsheet ID
const range = “Sheet1!A4”; // Replace with the sheet name and cell reference you want

_// Make a request to get the value of cell A4_
**const** response = **await** axios($, {
  url: `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/${range}`,
  headers: {
    Authorization: `Bearer ${**this**.google_sheets.$auth.oauth_access_token}`,
  },
});

_// Extract the value from the response_
**const** cellValue = response.data.values[0][0];

**return** cellValue;

},
});

i have tried this one and it returns a valid reply

Is the code you’re running throwing an error? Or is it not returning data that you expect it to?

TypeError
Cannot read properties of undefined (reading ‘values’)
DETAILS

    at Object.run (file:///tmp/__pdg__/dist/code/3ab4640a4d5dddcc6a5229fc9f7fc2566f540e6484c9846e9f36aa4d2917f93c/component.mjs:20:37)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at null.executeComponent (/var/task/launch_worker.js:294:22)
    at MessagePort.messageHandler (/var/task/launch_worker.js:791:28)

can you please describe what might cause a TypeError with the message “Cannot read properties of undefined (reading ‘values’)“?

The error message “Cannot read properties of undefined (reading ‘values’)” typically occurs in JavaScript when you’re trying to access a property or method on an object that is undefined. This can happen in various scenarios, especially when working with asynchronous operations or when dealing with data that may not be initialized or returned as expected. Here are a few common causes: 1. Accessing a property on an object that hasn’t been initialized: If you try to access a property or method on an object before it has been defined or assigned a value, you’ll encounter this error. For example: let myObject; console.log(myObject.values); // This will throw the TypeError 2. Asynchronous Data Fetching: When fetching data asynchronously (e.g., from an API), if you try to access a property on the response before the data has been fully fetched, you might encounter this error. Ensure you’re waiting for the data to be loaded before accessing its properties. async function fetchData() { const response = await fetch('https://api.example.com/data'); const data = await response.json(); console.log(data.values); // Make sure data is not undefined } 3. Incorrectly Accessing Nested Properties: If you’re trying to access a deeply nested property without checking if each level of the object exists, you might encounter this error. It’s good practice to check if each level is defined before accessing deeper properties. const user = { profile: undefined }; console.log(user.profile.details.name); // This will throw the TypeError // A safer approach: if (user.profile && user.profile.details) { console.log(user.profile.details.name); } 4. Misuse of Array Methods: If you’re working with array methods (e.g., map, filter, reduce) and you try to access a method on an element that is undefined, this error can occur. Ensure the array elements you’re working with are defined and have the properties you’re trying to access. const numbers = [1, 2, undefined, 4]; numbers.forEach(number => console.log(number.values())); // This will throw the TypeError To avoid this error, always ensure the object or variable you’re trying to access is defined and not undefined. You can use conditional checks, optional chaining (?.), or try-catch blocks to handle scenarios where data

tried most of this solutions and nothing worked. it strange because the built in steps for sheets work fine

is there anypossibility that pipedream blocks the api request?

can it be scope problem?

If there was a authorization error, then the response would be a 401, and you’d see a very explict error about that.

I would console.log(response) directly in the code before you attempt to navigate to specific rows within the response, just so you can see the actual shape of the data.

It might be possible that the request is successful, but there’s a minor typo in the selection or even the selected worksheet.