最近在开发个微信小程序,想通过wx.chooseImages,wx.cloud.uploadFile来获取图文件id,但是出现了很诡异的bug
先贴上自己写的代码
Page{
data:{
imagesID:[]
}
// 添加图片
addimages: function (e) {
var that = this
var img = that.data.imagesUrl
if(img.length<6){
var addtion = e.target.dataset.addimg
if (addtion == undefined || addtion == 0) {
wx.chooseImage({
count:6,
success: function (res) {
var tempFilePaths = res.tempFilePaths
img = img.concat(tempFilePaths)
for (var count = 0; count < that.data.imagesUrl.length;) {
that.uploadImg(count)
count++
}
that.setData({ imagesUrl: this.data.imagesID})
}
})
}
}
else {
wx.showModal({
title: '提示',
content: '您上传的图片数量已达到上限',
})
}
},
// 上传图片文件
uploadImg(count) {
let that = this
wx.cloud.uploadFile({
cloudPath: (new Date()).valueOf()+'.png',
filePath: this.data.imagesUrl[count],
}).then(res=>{
that.data.imagesID.push(res.fileID)
})
},
// 提交表单
submitForm(e) {
let that = this
for (var count = 0; count < that.data.imagesUrl.length;) {
that.uploadImg(count)
count++
}
console.log(that.data.imagesID)
}
if (res.requestType === "") {
wx.showToast({
title: '请选择需求类型',
image: 'close-circle.png'
})
}
else if (res.description == "") {
wx.showToast({
title: '请说明具体情况',
image: 'close-circle.png'
})
}
else {
if (res.requestType == '知识') {
school.add({
data: {
type: res.requestType,
description: res.description,
imagesUrl:that.data.imagesID,
}
}
}
代码大概就这样,同样在submitForm函数里都要执行先循环执行uploadImg()在不执行if、else if 直接else和执行if、else if 再else输出的那个that.data.imagesID的值前者shu'chu为默认的空数组,后者就正常被赋值了(还一定要console.log才行),大佬们帮我看看这究竟是什么问题…………,if else if只是调用了wx.showToast api,不会影响that.data.imagesID的输出