var app = getApp()
const db = wx.cloud.database()
Page({
data: {
bookInfo:null,
hidden:1,
tempImg: [],
fileIDs: [],
},
submit: function () {
wx.showLoading({
title: '提交中',
})
const promiseArr = []
for (let i = 0; i < this.data.tempImg.length;i++) {
let filePath = this.data.tempImg[i]
let suffix = /\.[^\.]+$/.exec(filePath)[0];
promiseArr.push(new Promise((reslove,reject)=>{
wx.cloud.uploadFile({
cloudPath: new Date().getTime() + suffix,
filePath: filePath,
}).then(res => {
console.log(res.fileID)
this.setData({
fileIDs: this.data.fileIDs.concat(res.fileID)
})
reslove()
}).catch(error => {
console.log(error)
})
}))
}
Promise.all(promiseArr).then(res=>{
db.collection('comments').add({
data: {
fileIDs: this.data.fileIDs
}
})
.then(res => {
console.log(res)
wx.hideLoading()
wx.showToast({
title: '提交成功',
})
})
.catch(error => {
console.log(error)
})
})
},
uploadImgHandle: function () {
wx.chooseImage({
count: 9,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success:res=> {
const tempFilePaths = res.tempFilePaths
this.setData({
tempImg: tempFilePaths
})
}
})
},
onLoad: function (options) {
},
onReady: function () {
},
})
<button type="warning" bindtap='uploadImgHandle'>上传图片</button>
<view>
<block wx:for="{{tempImg}}" wx:key="{{index}}tmpimg">
<image src='{{item}}' mode='aspectFill'></image>
</block>
</view>
<button type="primary" bindtap='submit'>提交</button>
报错代码是 reslove()
}).catch(error => {
console.log(error)