Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

297
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda