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

195
Visualizações
Object is possibly 'undefined' Reactjs

this question solution is already available in stackoverflow but, i am still confuse,i have use same as they answer still go this error please help me to get out of this error thank you my list of object is

 const typeValue = [
        {name: "Subscription", value: "subscription", rank: 2},
        {name: "Single", value: "single", rank: 3},
        {name: "Trial", value: "trial", rank: 0},
        {name: "Unlimited", value: "unlimited", rank: 1000},
        {name: "Promo", value: "promo", rank: 1},
        {name: "Test", value: "test", rank: 0},
        {name: "Brand", value: "brand", rank: 3}
    ]

any i want to find rank number

  rank: typeValue.find(x => x.value === snapshot.data().type).rank    
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

find will return undefined if it can't find a match, and then it is illegal to call .rank. To solve, use ?.rank instead:

rank: typeValue.find(x => x.value === snapshot.data().type)?.rank

If the problem persists, then you may have to do the same for .type:

rank: typeValue.find(x => x.value === snapshot.data()?.type)?.rank
about 4 years ago · Juan Pablo Isaza Relatório

0

typeValue.find(x => x.value === snapshot.data().type)

You're trying to find an element in typeValue that satisfies x.value === snapshot.data().type, but snapshot.data().type could be anything so TypeScript is warning that typeValue.find(x => x.value === snapshot.data().type) may return undefined.

If you want to tell TypeScript that x.value === snapshot.data().type will definitely match one of the elements, you can use !:

rank: typeValue.find(x => x.value === snapshot.data().type)!.rank

You can also use ? if rank can be undefined:

rank: typeValue.find(x => x.value === snapshot.data().type)?.rank
about 4 years ago · Juan Pablo Isaza Relatório

0

Declare types for you typeValue array variable :-

const typeValue: { name: string; value: string; rank: number }[] = [
  { name: "Subscription", value: "subscription", rank: 2 },
  { name: "Single", value: "single", rank: 3 },
  { name: "Trial", value: "trial", rank: 0 },
  { name: "Unlimited", value: "unlimited", rank: 1000 },
  { name: "Promo", value: "promo", rank: 1 },
  { name: "Test", value: "test", rank: 0 },
  { name: "Brand", value: "brand", rank: 3 },
];

Also add types for snapshot.data().type, if you didn't declared it already!

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