云函数:
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
const file = event.detail.file;
var fileID = []
file.forEach(element => {
var name = 'ershoujiaoyi/good/_' + wxContext.openid + '_' + String(Math.random()) + '.jpg'
cloud.uploadFile({
cloudPath: name,
fileContent: element.url,
success: res => {
console.log('上传成功:',res)
fileID = [...fileID, res.fileID]
},
fail: err => {
console.log('Error:', err);
return err
}
});
});
console.log('Complete:',fileID)
return fileID
}
云函数本地调试:
调用:
wx.cloud.callFunction({
name:'uploadGoodImage',
data:{
detail:event.detail
},
success:res=>{
console.log(res)
this.setData({
fileID:res.result
})
},
fail:err=>{
console.log(err)
},
complete:_=>{
wx.hideLoading()
}
})
控制台输出:
新人小程序求教,如有愚钝之处,烦请指教,在此先行谢过。
1、没有await的,大概率是异步原因。
2、云函数里尽量不要用forEach,用for()
for(let i=0;i<file.length;i++){ let res = await cloud.uploadFile() fileIDs = [...fileIDs, res.fileID] }
fileID是固定,没必要这么返回,uploadFile是异步处理,没必要await,云函数3s机制,否则报out time after 3s