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

162
Visualizações
Node.js Cannot read property 'toJsonWith' of undefined

I'm trying to return icons, as a result, to produce them on the page

The following code below map all the results that I'm trying to return to the page.

const icons = await Icon.find({ _id: { $in: ids } }).select({ ...options.select, ...options.additional })
    const results = ids
      .map(id => {
        const iconsGroup = icons.find(icon => icon.id === id)
        if (query.hasPublishingError) {
          iconsGroup.icons = iconsGroup.icons.filter(el => (el.errorsData.length > 0 && el.status === 'draft'))
        }
        return iconsGroup
      })
      .map(i => i.toJsonWith(options.select))

await postProcess(results)

    return {
      docs: results,
      limit: response.limit,
      page: response.page,
      pages: response.pages,
      total: response.total
    }

When I'm making a GET request, I'm getting an error such as:

TypeError: Cannot read property 'toJsonWith' of undefined\n

The painful point is .map(i => i.toJsonWith(options.select))

By the way, for some reason, this function toJsonWith cannot be found as a declaration somewhere. Please help me, I'm trying to figure out what's wrong here for 4 days.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

an _id should always be unique so

const iconsGroup = icons.find(icon => icon.id === id)

should return an arrayObject with a maximum of one Element.

Then you check for publishing errors

if (query.hasPublishingError) {
      iconsGroup.icons = iconsGroup.icons.filter(el => (el.errorsData.length > 0 && el.status === 'draft'))
    }

And here it can happen that iconsGroup is an empty array.

Next you make a second map to use your magical toJsonWith (i also don't know what it does).

You can combine these two maps and iterate direclty over your icons instead of using a map on your ids (following code gives you an array of icons depending of query.hasPublishingError):

const results = icons.map(icon => {
if (query.hasPublishingError && icon.errorsData.length > 0 && icon.status === 'draft') {
    return icon.toJsonWith(options.select)
} else {
    return icon.toJsonWith(options.select)
}
})
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