I have such a problem that cypress cannot find the properties. In response I get array. I want to find record and get CustomMessage. :) Do you notice where I am making a mistake:/ ?
Here is my code :)
cy.request({
method: 'GET',
url: 'url',
})
.then((resp) => resp.body)
.then((data) =>
data.find((element) => element['body']['Message']['Phone'] == phone)
)
.then((phone) => (otpCode = phone['body']['Message']['CustomMessage']))
Thanks for any help :)
The problem is nothing in the array matches phone
Try adding a guard
cy.request(...)
.then((resp) => resp.body)
.then((data) => {
const found = data.find((element) => element.body.Message.Phone === phone)
if (!found) throw 'Error: phone not found'
return found
})
.then((phone) => {
otpCode = phone.body.Message.CustomMessage
})
The way you have it originally, when data.find()
fails the whole array is passed on to the otpCode
extraction.