TypeError when running Apps Script

36 views
Skip to first unread message

Bart Hoogland

unread,
Nov 29, 2022, 4:02:13 PM11/29/22
to Google Apps Script Community
Hi everyone,

Hope you can help me out with an issue I've been having with one of the scripts that I'm using. Unfortunately I'm not versed in the apps script language, so I'm not sure how to go about fixing this. The error is as follows:

TypeError: Cannot read property 'name' of undefined
simplicateProjectData    @ SimplicateProjectData.gs:51

I'm guessing the "51" part refers to line 51 in my code? If so, this is what is written there:

arrayProjectManagerName.push([jsonObject.data[i].project_manager.name])

The above line is part of a var loop. I'd be happy to share the entire script if necessary.

Thanks in advance!

- Bart

Michael O'Shaughnessy

unread,
Nov 29, 2022, 9:12:18 PM11/29/22
to google-apps-sc...@googlegroups.com
Well, the error is saying that for some reason there is no "value" for the property "name" because it doesn't "exist".

The line that you share looks like it is trying to use the "dot notation" of an object called "jsonObject".  So I would try to debug or log out the json object to see it's structure.  You should be able to "walk" through the JSON object starting at "data".  This means you should be able to "traverse" the object in this pattern: data - project - manager - name.   

Here is a good site that might help you: https://dmitripavlutin.com/access-object-properties-javascript/ 


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/883f23a7-969a-4a95-99c2-c102528518ean%40googlegroups.com.

Bart Hoogland

unread,
Nov 30, 2022, 4:57:53 AM11/30/22
to Google Apps Script Community
Hey!

Thanks for the answer. Since I'm not much of a coder I'm struggling with comprehending exactly how to solve the issue and hope you can be of help. I'm thinking that perhaps the project_manager data has null values in it, so I'm thinking we need to add an if-then statement in the code. Something like: if the name of the project manager is empty, then fill the cell with "". Otherwise, use the project manager's name. How could I implement something like this? I was thinking along the lines of the following:

if(jsonObject.data[i].project_manager.name == undefined {
arrayProjectManagerName.push([""])
}else{
arrayProjectManagerName.push([jsonObject.data[i].project_manager.name])

Michael O'Shaughnessy

unread,
Dec 1, 2022, 6:46:58 PM12/1/22
to google-apps-sc...@googlegroups.com
I think you have a solid idea!  I will say that you might want to look at some sites to learn about comparison operators.  There is a difference between using "==" and "===".  The first is "loose" and the second is "strict".  You may want to stick with the "strict" version.  Here is a good site to give you some info about it: https://www.w3schools.com/Jsref/jsref_operators.asp

I still suggest "logging out" arrayProjectManager to see how it is structured.  This way you know for sure that the array (object) does in fact have the right structure for your "object.data.project.manager.name".  This is a rather "deep" object.  Now after I said that you might really be using (and most likely) an object that would be referred to as a JSON object. Here is a link you may want to have a look at: https://www.w3schools.com/js/js_json_intro.asp

Anyway, if you get a chance to share some of what is logged out we can take a closer look.

Bart Hoogland

unread,
Dec 2, 2022, 5:39:03 AM12/2/22
to Google Apps Script Community
Thanks again for the response. I've looked up the documentation and in fact it does look like the structure of the object matches the code. Another thing I forgot to add is that the script used to run fine until a few months ago. So it should be able to retrieve the data, but for some reason it throws me the TypeError as quoted in the original post.

Michael O'Shaughnessy

unread,
Dec 2, 2022, 8:41:08 PM12/2/22
to google-apps-sc...@googlegroups.com
Ahhhh!  The plot thickens!!  You have a JSON object that has other objects that are arrays of objeects!!!  Looking at the link you supplied this is just a little snippet of the output:
{ "data": [ { "id": "string", "project_manager": { "id": "string", "person_id": "string", "name": "string"

Notice the bracket after the colon after "data"?  Brackets are arrays!!!  So you can use "dot notation" up until the array.  You then need to use array bracket notation then back to dot notation.  Whew!!!


And copy and paste the following into a new script file and run both the functions.  You can see when you run jsonArray2 you will get "Brian May" then you will get an error message of "undefined".  I do believe this is what you are running into.  

let jsonArray = () => {
let dt = {
"cars": {
"Nissan": [
{ "model": "Sentra", "doors": 4 },
{ "model": "Maxima", "doors": 4 },
{ "model": "Skyline", "doors": 2 }
],
"Ford": [
{ "model": "Taurus", "doors": 4 },
{ "model": "Escort", "doors": 4 }
]
}
}
console.log(dt)
console.log(dt.cars.Nissan[0].model)
}

let jsonArray2 = () => {
let bt = {
"data": [
{
"id": "string",
"project_manager": {
"id": "string",
"person_id": "string",
"name": "Brian May"
},
"project_status": {
"id": "string",
"label": "string",
"color": "string"
}
}]
}
console.log(bt.data[0].project_manager.name)
console.log(bt.data.project_manager.name)

Give it a shot and let me know if you are still running into issues.

Reply all
Reply to author
Forward
0 new messages