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

206
Visualizações
Node Promise properties

I'm developing an importer, for each category i have to create the category and their sizes, for example in this input JSON:

const input_json = {
"categories": [
        {
            
            "title": "category title",
            "sizes": [
                {
                    
                    "title": "size title",
                    "slices": 8,
                    "flavors": 4,
                    
                },
                {
                    "id" : "",
                    "title": "size title 2",
                    "slices": 8,
                    "flavors": 4,
                    
                }
            ]
       }
   ]
}

but the problem is that sizes need to know the category id, so i'm trying to do it like this:

const prepareCategories = (body) => {
    let category_promises = []

    for (let i = 0; i < body.categories.length; i++) {
        const categoryBody = body.categories[i]

        let object_category = {
           ......
        } // set the object
        
        categoryPromise = nmCategorySvcV2.create(object_category) 
        
        categoryPromise.size = categoryBody.sizes

        category_promises.push(
            categoryPromise,
        )
    }
    
    return category_promises
}

......

          let category_promises = prepareCategories(input_json) // passing the input 

            Promise.all(category_promises).then((categories) => {
                console.log(categories)
                
            })

but when I see the result of Promise.all the sizes property is not displayed, only the properties of the category actually created.

what am i doing wrong?

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

0

You are setting the size on the promise (which doesn't have any meaning). Instead you need to wait for the promise to be resolved and then set the size on its result:

const categoryPromise = nmCategorySvcV2.create(object_category)
 .then(result => Object.assign(result, { size: categoryBody.sizes }))

(By the way you were missing a declaration for categoryPromise.)

The code could become a bit clearer using await syntax:

const prepareCategories = body => body.categories.map(async categoryBody => {
  const object_category = {
    //......
  } // set the object
  const categoryData = await nmCategorySvcV2.create(object_category)
  categoryData.size = categoryBody.sizes 
  return categoryData
})
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