我想在对象数组中执行promise,并用我在下面的代码尝试的同一数组中的promise响应替换数组中的路径值:
const cloudinary = require('cloudinary')
const cloudinaryConfig = require('../configs/cloudConfig.json')
cloudinary.config({
cloud_name: cloudinaryConfig.cloud_name,
api_key: cloudinaryConfig.api_key,
api_secret: cloudinaryConfig.api_secret
})
fileArray = [
{
fieldname: 'productThumbImage',
originalname: 'Boyka.png',
path: '/home/rahul/MaxDigiAssignment/mxNodeEcommerce/mx-ecommercenode/upload/product/1589983049420.png'
},
{
fieldname: 'productPhoto',
originalname: 'Code.png',
filename: '1589983049436.png',
path: '/home/rahul/MaxDigiAssignment/mxNodeEcommerce/mx-ecommercenode/upload/product/1589983049436.png'
},
{
fieldname: 'productPhoto',
originalname: 'Boyka.png',
filename: '1589983049438.png',
path: '/home/rahul/MaxDigiAssignment/mxNodeEcommerce/mx-ecommercenode/upload/product/1589983049438.png'
}
]
const files = imgs.map(async (img)=>{
let path = await cloudinary.v2.uploader.upload(img.path)
return {
...img,
filePath: path.url
}
})
console.log('files 11/// ',await files);
但是我得到了错误承诺{}
我想在每个对象中执行此承诺,然后获得响应我的预期数组如下:
[
{
fieldname: 'productThumbImage',
originalname: 'Boyka.png',
path:'http://res.cloudinary.com/deqpxepbs/image/upload/v1589982424/ywfetodkvyadmdqjof1i.png'
},
{
fieldname: 'productPhoto',
originalname: 'Code.png',
filename: '1589983049436.png',
path:'http://res.cloudinary.com/deqpxepbs/image/upload/v1589982424/desb18dgungva5y8apbv.png'
},
{
fieldname: 'productPhoto',
originalname: 'Boyka.png',
filename: '1589983049438.png',
path:'http://res.cloudinary.com/deqpxepbs/image/upload/v1589982424/ugud4flh7gfl3ymyh0xx.png'
}
]
files
将是一个诺言数组,它不是一个诺言。您应该:console.log('files 11/// ',await Promise.all(files));