โ09-14-2020 09:29 AM
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?
Solved! Go to Solution.
โ09-22-2020 04:09 AM
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:
Attached herewith is your pipeline with already implemented changes:
AC105 2_TESTer_2020_09_22.slp (30.1 KB)
Regards,
Lazo
โ09-22-2020 04:09 AM
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:
Attached herewith is your pipeline with already implemented changes:
AC105 2_TESTer_2020_09_22.slp (30.1 KB)
Regards,
Lazo