Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
Use return value in a .map after performing request.get

So basically, due to the security, I need to convert image (from the url) to the base64.

So far I have two functions. One function is converting the image from the url to the Base64 and the other one is mapping over the database and is replacing the default url to the base64 format. I miss the last piece of the puzzle, how to use the return value from the 1st function in the second one - I want to replace 'test' in the second function with the result of conversion from url to base64.

 public async convertUrlToBase64(): Promise<any> {
    const request = require('request').defaults({ encoding: null });

    await request.get(
      'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Ray_and_Maria_Stata_Center_%28MIT%29.JPG/2560px-Ray_and_Maria_Stata_Center_%28MIT%29.JPG',
      function (
        error: any,
        response: { statusCode: number; headers: { [x: string]: string } },
        body: ArrayBuffer | SharedArrayBuffer,
      ) {
        return 'data:' + response.headers['content-type'] + ';base64,' + Buffer.from(body).toString('base64');
      },
    );
  }

  public mapUriToBase64(note: any): any {
    return {
      ...note,
      images: note.images.map((image: File) => {
        return {
          ...image,
          uri: 'test',
        };
      }),
    };
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You basically need to wrap it with the Promise in order to get the returned value from your call.

public convertUrlToBase64(): any {
    const request = require('request').defaults({ encoding: null });

    return new Promise(function (resolve, reject) {
      request.get(
        'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Ray_and_Maria_Stata_Center_%28MIT%29.JPG/2560px-Ray_and_Maria_Stata_Center_%28MIT%29.JPG',
        function (
          error: any,
          response: { statusCode: number; headers: { [x: string]: string } },
          body: ArrayBuffer | SharedArrayBuffer,
        ) {
          const data = 'data:' + response.headers['content-type'] + ';base64,' + Buffer.from(body).toString('base64');
          resolve(data);
        },
      );
    });
  }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda