TypeError: Cannot read property ‘length‘ of undefined

文章目录:

1.问题代码

2.出现的问题

3.问题分析

4.解决办法


1.问题代码

            //判断有没有需要上传的图片数组
            if(chooseImgs.length != 0){
            // if (chooseImages!=0) {    
            chooseImgs.forEach((v,i)=>{
            wx.uploadFile({
              url: 'https://img.coolcr.cn/api/upload',
                //   url: 'http://my.zol.com.cn/index.php?c=Ajax_User&a=uploadImg',
              //https://imgchr.com/i/MjaXxU
              filePath: v,
              name: 'file',
              formData:{},
              success:(result)=>{
                //   console.log(result);
                  let url=JSON.parse(result.data).data.url;
                  this.UpLoadImgs.push(url);
             // 4文本域和外网的图片的路径一起提交到服务器前端的模拟不会发送请求到后台。
             if(i===chooseImgs.length-1){
                wx.hideLoading();
                console.log("把文本的内容和外网的图片数组 提交到后台中");
            // 5清空当前页面
                this.setData({
                    textVal:"",
                    chooseImgs:[]
                })
            // 6返回上一页
                wx.navigateBack({
                    delta:1
                });
             }
              },
              fail: (err) => {}
            });
         })

        }else{
            wx.hideLoading();
            console.log("只是提交了文本");
            // this.setData({
            //     TextValue:""
            // })
            wx.navigateBack({
                delta:1
            })
        }

2.出现的问题

3.问题分析

WAServiceMainContext.js:2 TypeError: Cannot read property 'length' of undefined



'length' of undefined,那么对应的是chooseImgs
所以我们需要对他进行判定才能解决
if(chooseImgs){

}

4.解决办法

 if(chooseImgs){
                if(chooseImgs.length != 0){
                    // if (chooseImages!=0) {    
                    chooseImgs.forEach((v,i)=>{
                    wx.uploadFile({
                      url: 'https://img.coolcr.cn/api/upload',
                        //   url: 'http://my.zol.com.cn/index.php?c=Ajax_User&a=uploadImg',
                      //https://imgchr.com/i/MjaXxU
                      filePath: v,
                      name: 'file',
                      formData:{},
                      success:(result)=>{
                        //   console.log(result);
                          let url=JSON.parse(result.data).data.url;
                          this.UpLoadImgs.push(url);
                     // 4文本域和外网的图片的路径一起提交到服务器前端的模拟不会发送请求到后台。
                     if(i===chooseImgs.length-1){
                        wx.hideLoading();
                        console.log("把文本的内容和外网的图片数组 提交到后台中");
                    // 5清空当前页面
                        this.setData({
                            textVal:"",
                            chooseImgs:[]
                        })
                    // 6返回上一页
                        wx.navigateBack({
                            delta:1
                        });
                     }
                      },
                      fail: (err) => {}
                    });
                 })
        
                }else{
                    wx.hideLoading();
                    console.log("只是提交了文本");
                    // this.setData({
                    //     TextValue:""
                    // })
                    wx.navigateBack({
                        delta:1
                    })
                }
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘鑫磊up

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值