小程序想通过上传图片后将图片位置存储在云函数的字段里,但返回的结果是个[]空的值,无法保存在云函数字段里,可能是异步问题
运行结果及报错内容
async chooseImg(e) {
await this.chooseImg2(e)
await wx.cloud.callFunction({
name: "deliver_table",
data: {
id: this.data.dataList[e.currentTarget.dataset.index]._id,
deliver_place: this.data.dataList[e.currentTarget.dataset.index].deliver_place
},
})
},
async chooseImg2(e) {
let that = this
var val = e.detail.value;
let index = e.currentTarget.dataset.index
console.log(index)
wx.chooseImage({
count: 2,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success(res) {
// tempFilePath可以作为 img 标签的 src 属性显示图片
const tempFilePaths = res.tempFilePaths
console.log(tempFilePaths)
for (let i = 0; i < tempFilePaths.length; i++) {
wx.cloud.uploadFile({
cloudPath: "deliver_place/" + Date.now() + ".jpg",
filePath: tempFilePaths[i]
}).then(res => {
console.log(res)
let deliver_place = 'dataList[' + index + '].deliver_place[' + i + ']'
that.setData({
[deliver_place]: res.fileID
})
console.log(that.data.dataList)
})
}
}
})
},