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

306
Visualizações
async await for fs writeFile

I am getting array of objects which contain base 64 images . I have to convert base64 images into url and have to save in db. But the problem i i have a loop which contain a single image and convert string to url. But i am getting issue of await . Because the file is still writing string to url but loop move to other file so i am not able to propely save the all files. Here is my code

let attachments =[]
  for (let i = 0; i < files.length; i++) {
            //Get Base64
            if (
              files[i].base64 &&
              files[i].base64 != null &&
              files[i].base64 != ""
            ) {
              let base64 = files[i].base64.split(";base64,").pop();
              let path = `uploads/${Date.now()}-${files[i].name}`;
              fs.writeFile(
                `${path}`,
                base64,
                {
                  encoding: "base64",
                },
                async function (err) {
                  if (err) {
                    console.log(err);
                    return res.status(400).send(err.message);
                  } else {
                    attachments.push({
                      attachmentTitle: files[i].attachmentTitle,
                      eAttachmentT: files[i].attachmentTitle,
                      eAttachmentPath: path,
                      eAttachmentURL: path,
                      description: files[i].description,
                    });

                    await Model.findOneAndUpdate(
                      {
                        "level1.email": req.body.email,
                      },
                      {
                        $set: {
                          "level2.attachments": attachments,
                        },
                      }
                    ).then(async (respo) => {
                      // console.log("up");
                      // console.log(attachments);
                      if (files.length === i + 1) {
                       
                        if (Model.level1.supplierStatus == "REJECTED") {
                          await axios({
                            method: "post",
                            url: `${process.env.SELF}/rgt`,
                            data: {
                              vendorId: respo._id,
                            },
                          })
                            .then((resp) => {
                              return res.status(200).send(resp.data);
                            })
                            .catch((err) => {
                              return res.status(400).send(err);
                            });
                        } else {
                          let result = await Model.findOne({
                            "level1.email": req.body.email,
                          });
                          return res
                            .status(200)
                            .send(result.level2.attachments);
                        }
                      } 
                    });
                  }
                }
              );
            } 
about 4 years ago · Juan Pablo Isaza
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