Hello. Just started writing some advanced scripts. Fencing an issue:
My array could have from 2 to infinite number of entities. My task is find parse the originalPOSIdentity from response and to compare it to some value. One of arrays do not have the originalPOSIdentity property. That`s why I get Cannot read property ‘originalPOSIdentity’ of undefined.
Body:
{
"receipt": {
"delta": false,
"entities": [
{
"offset": {
"totalSeconds": 0,
"id": "Z",
"rules": {
"transitionRules": [],
"transitions": [],
"fixedOffset": true
}
},
"hour": 18,
"minute": 56,
"nano": 627000000,
"second": 37,
"dayOfMonth": 3,
"dayOfWeek": "WEDNESDAY",
"month": "FEBRUARY",
"year": 2021,
"dayOfYear": 34,
"monthValue": 2
},
"id": 0,
"input": false,
"invalidated": false,
"layout": {
"type": "DSL",
{
"cells": [
{
"text": "SA: 1",
"width": 45
},
{
"text": "Store: 9101",
"width": 35
},
{
"align": "RIGHT",
"text": "No: 31",
"width": 20
}
]
},
{
"cells": [
{
"text": "Date: 2021-02-03",
"width": 45
},
{
"text": "Till: 99",
"width": 30
},
{
"align": "RIGHT",
"text": "Time: 20:56",
"width": 25
}
]
},
{
"cells": [
{
"text": "-",
"truncate": "CUT",
"type": "TEXT_FILL",
"width": 100
}
]
}
]
},
"type": "DECORATOR"
},
{
"id": 13,
"input": true,
"invalidated": false,
"layout": {
"type": "DSL",
},
"saleItem": {
"actualUnitPrice": 100.00,
"extendedAmount": 100.00,
"name": "***",
"originalPOSIdentity": "02000011111019",
"quantity": {
"unitOfMeasure": "EA",
"value": 1.00
}
},
"type": "SALE_ITEM"
},...
Temporary solution:
var originalPOSIdentity = pm.environment.get("item1");
for (i in data.receipt.entities) {
if (data.receipt.entities[i].type === "SALE_ITEM") {
if (data.receipt.entities[i].saleItem.originalPOSIdentity === originalPOSIdentity) {
pm.environment.set("entityId", data.receipt.entities[i].id);
}
}
}
By I know that there is a better way to find the originalPOSIdentity. Can someone help me?