Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

295
Vistas
Cypress- cannot read properties of undefined

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:/ ? console response

cypress error

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 :)

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can get the customMessage like this:

cy.request({
  method: 'GET',
  url: 'url',
}).then((resp) => {
  const customMessage = resp.body[1].body.Message.customMessage
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

Make it simple with cy-spok.

const 'spok' = require('cy-spok')


cy.request({
    method: 'GET',
    url: 'url',
  })
  // access properties with .its()
  .its('response.body')
  .its(1)
  .should(spok({
    Message: {
      Phone: spok.string // or us spok.test to check string length as well
    }
  // now we verified the response has Message.Phone let's extract it
  .its('Message.Phone')
  .as('otpCode')
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda