收藏
回答

为什么会出现Cannot read property 'concat' of undefined?

用云开发上传图片,其中一个项目运行正常,但另一个项目运用的时候报错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)
        })
      }))
    }

一个项目运行成功,另一个项目运行报错

回答关注问题邀请回答
收藏

2 个回答

  • Mr.Zhao
    Mr.Zhao
    2020-03-19
    Page({
    
      data: {
    
        tempImg: [],
    
        fileIDs: []
    
      },
    
      uploadImgHandlefunction () {
    
        wx.chooseImage({
    
          count9,
    
          sizeType: ['original''compressed'],
    
          sourceType: ['album''camera'],
    
          successres => {
    
            const tempFilePaths = res.tempFilePaths
    
            this.setData({
    
              tempImg: tempFilePaths
    
            })
    
          }
    
        })
    
      },
    
      
    
      submitfunction () {
    
        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({
    
              cloudPathnew Date().getTime() + suffix,
    
              filePath: filePath, // 文件路径
    
            }).then(res => {
    
              this.setData({
    
                fileIDsthis.data.fileIDs.concat(res.fileID)
    
              })
    
              reslove()
    
            }).catch(error => {
    
              console.log(error)
    
            })
    
          }))
    
        }
    
      },
    
    })
    
    
    
    
    2020-03-19
    有用 1
    回复 2
    • 木 示
      木 示
      2020-03-19
      问题解决了,刚刚把页面清空了重新写进去就可以了。十分感谢大神的帮助
      2020-03-19
      回复
    • 安
      2020-03-29回复木 示
      求问怎么解决的呀?
      2020-03-29
      回复
  • 木 示
    木 示
    2020-03-19
    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:[]
              })
            },
          })
        })
      },
    )}
    


    2020-03-19
    有用
    回复
登录 后发表内容
问题标签