cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Script Snap: TypeError: Cannot read property "Question_ID" from undefined

acesario
Contributor II

I have a script snap which uses javascript to transform/restructure the input data. When I do this by hard coding the input data, my script works fine. The data in my hardcoded variable is exactly what I copy from the raw data input in snaplogic.

However, when I use the input doc rather than the hard coded input, my script fails with the error Cannot read property โ€œQuestion_IDโ€ from undefined.

Input to the script snap:
[{โ€œanswerTextโ€:โ€œ-โ€,โ€œQuestion_IDโ€:โ€œBOX_OR_RESIDENCEโ€},{โ€œanswerTextโ€:โ€œ02/29/2000โ€,โ€œQuestion_IDโ€:โ€œDATE_OF_BIRTHโ€},{โ€œanswerTextโ€:โ€œ5C_First_0912_001โ€,โ€œQuestion_IDโ€:โ€œFIRST_NAMEโ€},{โ€œanswerTextโ€:โ€œFโ€,โ€œQuestion_IDโ€:โ€œGENDERโ€},{โ€œanswerTextโ€:โ€œ2022โ€,โ€œQuestion_IDโ€:โ€œGRAD_YEARโ€}]

Desired output:
[{โ€œRequestโ€:{โ€œBOX_OR_RESIDENCEโ€:โ€œ-โ€,โ€œDATE_OF_BIRTHโ€:โ€œ02/29/2000โ€,โ€œFIRST_NAMEโ€:โ€œ5C_First_0912_001โ€,โ€œGENDERโ€:โ€œFโ€,โ€œGRAD_YEARโ€:โ€œ2022โ€}}]

This is the code in my script which is failing:
var result = {};
for (var i = 0; i < doc.length; i++) {
result[doc[i].Question_ID] = doc[i].answerText;
}

Attached is the pipeline, with a working and failing path.script.
AC105 2_TESTer_2020_09_14.slp (29.7 KB)

What am i doing wrong here with regards to the input doc?

1 ACCEPTED SOLUTION

lazo_ilijoski
New Contributor III

Hi @acesario,

The doc in the OK script is array, but it is not an array in the script that falls, so you cannot access the fields in the same way. Thus, if you want to get value of Question_ID you have to use the following doc.Question_ID, or in your case it will be:
result[doc.Question_ID] = doc.answerText;

Iโ€™ve changed your script and after the changes the document in the output of the script is as follows:
image

Attached herewith is your pipeline with already implemented changes:
AC105 2_TESTer_2020_09_22.slp (30.1 KB)

Regards,
Lazo

View solution in original post

1 REPLY 1

lazo_ilijoski
New Contributor III

Hi @acesario,

The doc in the OK script is array, but it is not an array in the script that falls, so you cannot access the fields in the same way. Thus, if you want to get value of Question_ID you have to use the following doc.Question_ID, or in your case it will be:
result[doc.Question_ID] = doc.answerText;

Iโ€™ve changed your script and after the changes the document in the output of the script is as follows:
image

Attached herewith is your pipeline with already implemented changes:
AC105 2_TESTer_2020_09_22.slp (30.1 KB)

Regards,
Lazo