等待承诺的结束循环?
waiting the ending loop of promise?
我怎么能等到循环结束,获取图像的路径,
然后将其注入到查询中?
创建数据前:
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,
} );
} );
我找不到解决方案。
谢谢。
您可以切换到使用 await
并像这样使用 fs 函数的承诺版本:
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;
}
并且,包含函数需要标记为 async
。
我怎么能等到循环结束,获取图像的路径, 然后将其注入到查询中?
创建数据前:
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,
} );
} );
我找不到解决方案。
谢谢。
您可以切换到使用 await
并像这样使用 fs 函数的承诺版本:
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;
}
并且,包含函数需要标记为 async
。