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

123
Visualizações
cannot read property of undefined using default value

I have a model that gets some data and filters. When the app starts I want to have all filters equal to true. I have set a default value but I still get the following error.

const model = (data, predictions, { placeFilter = true, startDateFilter = true, endDateFilter = true }) => {
   const getData = () => data.filter(placeFilter).filter(startDateFilter).filter(endDateFilter)
}
TypeError: Cannot read properties of undefined (reading 'placeFilter')
    at model (model.js:1)
    at init (index.js:15)
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Your parameter could be named to access the default value

const model = (data, predictions, filter = { placeFilter: true, startDateFilter: true, endDateFilter:  true }) => {
   const getData = () => data.filter(filter.placeFilter).filter(filter.startDateFilter).filter(filter.endDateFilter)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

const model = (data, predictions, { placeFilter = true, startDateFilter = true, endDateFilter = true }) => {
   const getData = () => data.filter(placeFilter).filter(startDateFilter).filter(endDateFilter)
}

You cannot call the above model function by passing two arguments like model([], []) as JavaScript will try to destructure the third argument which is undefined.

You can do this instead:

const model = (data, predictions, { placeFilter = true, startDateFilter = true, endDateFilter = true } = {}) => {
  console.log(placeFilter, startDateFilter, endDateFilter)
}

model([], [])
model([], [], { placeFilter: false })


BTW Assuming data is an array and you're trying to use Array.prototype.filter, filter should be called with a callback function that returns a boolean but you're directly passing booleans to all filters.

about 4 years ago · Juan Pablo Isaza Relatório

0

const model = (data, predictions, { placeFilter = true, startDateFilter = true, endDateFilter = true } = {}) => {
  const getData = () => data.filter(placeFilter).filter(startDateFilter).filter(endDateFilter)
}

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