Problem with asynchronous call

Asked

Viewed 44 times

1

Good evening guys, all right?

I’m in a boring situation and I don’t know how to fix it...

The thing is, I’m developing an API to bridge the data that’s in a database to a third-party system, which means my API queries the data in my database and returns an array of objects, to each object of this array is a POST that I need to do in the system.

I currently consult and make a map with Promise.all.. I iterate for each object of the array and send a POST to the other system, but at the moment that makes the POST it does not wait for the return of my client’s system and already considers as if it had been successful.

I can not know if it worked or wrong the request because it already jumps to the next iteration, if I try to return the Response pro Return (File that performs the POST) it considers as error.

I’ll leave the code below.

Thank you so much from the start, guys,

Thanks!

1. Arquivo da Iteração

Promise.all(integracoes.map(async int => {
  return await webserviceController.enviaRequisicao(
      int.INT_CH_TIPO_OPERACAO,
      int.INT_ST_TIPO_DOC,
      int
    );
})).then(response => {
  return res
  .status(200)
  .json({ Mensagem: 'Registros integrados com sucesso' });
}).catch(reject => {
  return res.status(400).json({
    message: 'Erro na execução do método',
    error: reject
  });
});

2. Arquivo do envio dos objetos do array

async criaDocumento(request) {
  try {
    const { ANEXOS: anexos, ...rest } = request;

    const [requestJSONDocument, fieldsDocument] = removeFields(rest, 'D');

    const response = await api.post('/NotasFiscais', requestJSONDocument, {
      headers: {
        "Content-Type": "application/json",
        authorization: `Bearer ${token}`
      }
    });

    const { id } = response.data;

    const responseJSON = { CUS_IN_IDWS: id , ...fields };

    return responseJSON;

  } catch (error) {
    throw 'Erro ao tentar integrar a Nota Fiscal.';
  }
}



  • On line 9 this 'Sponse' is being used? It seems that you created it only that is passing as 'res''

  • In case I’m not using, would you have to treat in that case be? Only when I try to pass pro result the responseJSON JSON pro error Return variable =/ It will be if it is not possible to use async with the map?

  • tries to give a console.log in this 'Replay', the data can be stored inside it

  • Which database are you pulling the data into your database?

  • I pull the data from Oracle, but I believe I managed to solve here man, thank you very much for the attention!!! Hug.

  • @Matheuslima you can post the answer to your question, it can help other people.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.