用云开发上传图片,其中一个项目运行正常,但另一个项目运用的时候报错Cannot read property 'concat' of undefined,为什么同一段代码在不同项目上会有不同结果?求大神帮忙看看,具体代码如下,js代码:
uploadImgHandle: function () {
wx.chooseImage({
count: 9,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: res => {
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePaths = res.tempFilePaths
this.setData({
tempImg: tempFilePaths
})
}
})
},
submit: function () {
const promiseArr = []
//只能一张张上传 遍历临时的图片数组
for (let i = 0; i < this.data.tempImg.length; i++) {
let filePath = this.data.tempImg[i]
let suffix = /\.[^\.]+$/.exec(filePath)[0]; // 正则表达式,获取文件扩展名
//在每次上传的时候,就往promiseArr里存一个promise,只有当所有的都返回结果时,才可以继续往下执行
promiseArr.push(new Promise((reslove, reject) => {
wx.cloud.uploadFile({
cloudPath: new Date().getTime() + suffix,
filePath: filePath, // 文件路径
}).then(res => {
// get resource ID
console.log(res.fileID)
this.setData({
fileIDs: this.data.fileIDs.concat(res.fileID)
})
reslove()
}).catch(error => {
console.log(error)
})
}))
}
一个项目运行成功,另一个项目运行报错
Page({ data: { tempImg: [], fileIDs: [] }, uploadImgHandle: function () { wx.chooseImage({ count: 9, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success: res => { const tempFilePaths = res.tempFilePaths this.setData({ tempImg: tempFilePaths }) } }) }, submit: function () { 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 => { this.setData({ fileIDs: this.data.fileIDs.concat(res.fileID) }) reslove() }).catch(error => { console.log(error) }) })) } }, })
let del = '' let jihe = '' const db = wx.cloud.database() Page({ data: { tempImg: [], fileIDs: [], imageurl: [], tempImg: [], fileIDs: [], dataList: [], }, delData(event) { del = event.detail.value console.log(del) }, deljihe(event) { jihe = event.detail.value console.log(jihe) }, // 上传及添加内容模块 uploadImgHandle: function () { wx.chooseImage({ count: 9, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success: res => { // tempFilePath可以作为img标签的src属性显示图片 const tempFilePaths = res.tempFilePaths this.setData({ tempImg: tempFilePaths }) } }) }, submit: function () { const promiseArr = [] //只能一张张上传 遍历临时的图片数组 for (let i = 0; i < this.data.tempImg.length; i++) { let filePath = this.data.tempImg[i] let suffix = /\.[^\.]+$/.exec(filePath)[0]; // 正则表达式,获取文件扩展名 //在每次上传的时候,就往promiseArr里存一个promise,只有当所有的都返回结果时,才可以继续往下执行 promiseArr.push(new Promise((reslove, reject) => { wx.cloud.uploadFile({ cloudPath: new Date().getTime() + suffix, filePath: filePath, // 文件路径 }).then(res => { // get resource ID console.log(res.fileID) console.log(this.data.fileIDs) this.setData({ fileIDs: this.data.fileIDs.concat(res.fileID) }) reslove() }).catch(error => { console.log(error) }) })) } Promise.all(promiseArr).then(res => { let that = this db.collection(jihe).add({ data: { itEm:'742', password:del, time: db.serverDate(), fileIDs: that.data.fileIDs //只有当所有的图片都上传完毕后,这个值才能被设置,但是上传文件是一个异步的操作,你不知道他们什么时候把fileid返回,所以就得用promise.all }, success(res){ console.log('添加成功') that.setData({ fileIDs:[] }) }, }) }) }, )}