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

169
Vistas
waiting the ending loop of promise?

How could I wait for the end of the loop, get the images' paths, and then inject it into the query?

Before data creation:

let images = [];
for ( const file in files ) {
    fs.writeFileSync(  __dirname + '/'+file+'.jpg', files[file].data );
    uploads( __dirname + '/'+file+'.jpg', 'smoothies/recipes' )
        .then( ( resolve ) => {
            images.push('https://res.cloudinary.com/###########' + resolve.public_id) ;
        } )
        .then( () => {
            console.log('images uploaded here path => ', images);
            fs.unlinkSync(  __dirname + '/'+file+'.jpg' );
            console.log('file removed here ');
            //file removed
        })
        .catch( ( error ) => {
            res.status( 500 ).send( {
                message:"failure tu upload the image",
                error,
            } );
        } );
}

Recipe.create( { name, ingredients, steps, images } )
    .then( async () => {
        let recipes = await Recipe.find();
        res.status( 201 ).json( recipes );
    } )
    .catch( ( error ) => {
        res.status( 500 ).send( {
            message:"failure to create the Recipe with images",
            error,
        } );
    } );

I can't find the solution.

Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can switch to using await and use the promise versions of the fs functions like this:

const fsp = fs.promises;

let images = [];
try {
    for (const file in files) {
        const filename = __dirname + '/' + file + '.jpg';
        await fsp.writeFile(filename, files[file].data);
        let resolve = await uploads(filename, 'smoothies/recipes');
        images.push('https://res.cloudinary.com/###########' + resolve.public_id);
        console.log('images uploaded here path => ', images);
        await fsp.unlink(filename);
        console.log('file removed here ');
    }
} catch (error) {
    res.status(500).send({
        message: "failure tu upload the image",
        error,
    });
    return;
}

try {
    await Recipe.create({ name, ingredients, steps, images });
    let recipes = await Recipe.find();
    res.status(201).json(recipes);
} catch (error) {
    res.status(500).send({
        message: "failure to create the Recipe with images",
        error,
    });
    return;
}

And, the containing function would need to be tagged as async.

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