收藏
回答

Cannot set property ‘’ of undefined;

问题模块
API和组件

Hi All,

我在Data 里面定议了:

 persons:[{My_Names:'',WX_Pics:'',My_Enroll_Years:'',My_Majors:'',My_Business:'',My_Phones:''}]


但在一下wx.request 的success里面对返回的字符串进行解释后,想对persons进行循环赋值。


在一个For的语句里写:

persons[i].My_Names = My_Name_temp;

报如下的错。

thirdScriptError

Cannot set property 'My_Names' of undefined;at api request success callback function

TypeError: Cannot set property 'My_Names' of undefined


试着改成:

persons[0].My_Names = My_Name_temp;就没有报错。但循环语句不能直接写成这样0,1,2......


如何搞?


最后一次编辑于  2017-02-05  (未经腾讯允许,不得转载)
邀请回答
复制链接收藏关注问题回答

3 个回答

  • 刘关林
    刘关林
    2017-02-06

    醉了,你这数组长度就是1,还用for循环,persons[0]以外的当然都是null




    2017-02-06
    赞同
    回复
  • snail
    snail
    2017-02-07

    有道理!我把{}里面的东西重复了10次,就没这报错了!

    但这样定义data,好像有点怪怪的。有没有简洁的写法。


    persons:[{My_Names:'',WX_Pics:'',My_Enroll_Years:'',My_Majors:'',My_Business:'',My_Phones:''},{My_Names:'',WX_Pics:'',My_Enroll_Years:'',My_Majors:'',My_Business:'',My_Phones:''},{My_Names:'',WX_Pics:'',My_Enroll_Years:'',My_Majors:'',My_Business:'',My_Phones:''},{My_Names:'',WX_Pics:'',My_Enroll_Years:'',My_Majors:'',My_Business:'',My_Phones:''},{My_Names:'',WX_Pics:'',My_Enroll_Years:'',My_Majors:'',My_Business:'',My_Phones:''},{My_Names:'',WX_Pics:'',My_Enroll_Years:'',My_Majors:'',My_Business:'',My_Phones:''},{My_Names:'',WX_Pics:'',My_Enroll_Years:'',My_Majors:'',My_Business:'',My_Phones:''},{My_Names:'',WX_Pics:'',My_Enroll_Years:'',My_Majors:'',My_Business:'',My_Phones:''},{My_Names:'',WX_Pics:'',My_Enroll_Years:'',My_Majors:'',My_Business:'',My_Phones:''},{My_Names:'',WX_Pics:'',My_Enroll_Years:'',My_Majors:'',My_Business:'',My_Phones:''}],

    2017-02-07
    赞同
    回复
  • 。
    2017-10-12

    一楼正解!

    2017-10-12
    赞同
    回复