0

I am trying to reach out to the "testdata":"two" property values [like 'EH']. While I was taking that property, I am facing "Cannot read property" this issue, How I fix this issue in Cypress?

[{
        "testdata": "one",
        "TC": {
            "EHQ0": "Address",
            }

    },
    {
        "testdata": "two",
        "TC": {
            "EH": "Student",
            "E1": "Question For Name",
            "EnglishText_E1": "Question For Name"
        }

    }
]

1 Answers1

2

You have an array of objects, so either index or find.

const data = [{
        "testdata": "one",
        "TC": {
            "EHQ0": "Address",
            }

    },
    {
        "testdata": "two",
        "TC": {
            "EH": "Student",
            "E1": "Question For Name",
            "EnglishText_E1": "Question For Name"
        }

    }
]

const objTwo = data.find(obj => obj.testdata === 'two')
const ehProp = objTwo.TC.EH
Raglan
  • 141
  • 7
  • I have implemented this concept in cypress. but I was getting the below error "data.find is not a function". I can't able to update the screenshot here – Elango Mano Dec 15 '22 at 04:36
  • describe('Retrive a json Property', () => { let sdata before(()=> { cy.fixture('filename').then(function(ldata){ sdata=ldata }); }); it('TC-01_StudentDetails', () => { cy.log(sdata.testdata.TC) sdata.forEach(data => { cy.log(data) if(data.testdata=="two") { cy.log(data.testdata.TC.EHQ0) } }) }) }) I have Updated my code. I am using the fixture to retrieve the JSON data in cypress, So, I can't able to set a const data in JSON file and it was taking an error – Elango Mano Dec 15 '22 at 05:17
  • TypeError data.find is not a function cypress/e2e/TestCases/TC-1.cy.js:36:23 34 | const objTwo = data.find(obj =>obj.testdata =='TestcaseaName') | ^ 37 | const ehProp = objTwo.TC.EH 38 | cy.log(ehProp) 39 | // cy.log(data.testdata) – Elango Mano Dec 15 '22 at 11:11