收藏
回答

在onLoad(options)中,如下错误,求解

thirdScriptError

Cannot read property 'inviteCode' of undefined;at pages/index/index page lifeCycleMethod onLoad function

TypeError: Cannot read property 'inviteCode' of undefined

    at t.onLoad (http://127.0.0.1:61317/appservice/pages/index/index.js:682:18)

    at t.<anonymous> (http://127.0.0.1:61317/appservice/__dev__/WAService.js:18:21400)

    at t.onShow (http://127.0.0.1:61317/appservice/pages/index/index.js:901:10)

    at t.<anonymous> (http://127.0.0.1:61317/appservice/__dev__/WAService.js:18:21400)

    at Kt (http://127.0.0.1:61317/appservice/__dev__/WAService.js:19:5408)

    at en (http://127.0.0.1:61317/appservice/__dev__/WAService.js:19:7211)

    at nn (http://127.0.0.1:61317/appservice/__dev__/WAService.js:19:7549)

    at Function.<anonymous> (http://127.0.0.1:61317/appservice/__dev__/WAService.js:19:9990)

    at X.<anonymous> (http://127.0.0.1:61317/appservice/__dev__/WAService.js:18:9835)

    at X.emit (http://127.0.0.1:61317/appservice/__dev__/WAService.js:7:31757)




报错代码为:

onLoad: function (options) {

var page = this;

//分享获取参数“邀请码”,存为全局变量

// console.log(app.globalData.inviteCode +"-1-1号")

// console.log(options.inviteCode +"-1-2号")

if (app.globalData.inviteCode == '' || app.globalData.inviteCode==undefined){

// console.log(options.inviteCode +"-1-3号")

if (options.inviteCode != '' && options.inviteCode != undefined) {   //读到这一行就报错

app.globalData.inviteCode = options.inviteCode;

}

console.log(app.globalData.inviteCode +"-1-4号")

}

},


最后一次编辑于  2018-09-14
回答关注问题邀请回答
收藏

2 个回答

  • 🚗🚕🚙🚌🚎🚒🚐🚚
    🚗🚕🚙🚌🚎🚒🚐🚚
    2018-09-14

    optionsapp.globalData有一个为undefined,看下你的app.js?还有你的onShow?

    2018-09-14
    赞同
    回复 5
    • AAA000-程序员的可乐
      AAA000-程序员的可乐
      2018-09-15

      onShow: function () {

      var page=this;

      page.onLoad();

      },


      2018-09-15
      回复
    • AAA000-程序员的可乐
      AAA000-程序员的可乐
      2018-09-15回复AAA000-程序员的可乐

      var api = require('/api.js');

      App({

      globalData: {

      userInfo: null,

      // ip: 'http://wap.09.com/',

      code: '',

      ip:'',

      mobile:'',

      UserToken:'',

      isLogin:false,

      openId:'',

      myInviteCode:'',

      inviteCode:''

      },

      onLaunch: function (options) {

      // console.log(options.inviteCode)

      // console.log(this.globalData.inviteCode)

      if(this.globalData.inviteCode==''){

      this.globalData.inviteCode = options.inviteCode;

      }

      // console.log(this.globalData.inviteCode);

      // 展示本地存储能力

      // var logs = wx.getStorageSync('logs') || []

      // logs.unshift(Date.now())

      // wx.setStorageSync('logs', logs)

      wx.clearStorageSync();

      // 登录

      wx.login({

      success: res => {

      // 发送 res.code 到后台换取 openId, sessionKey, unionId

      // console.log(res);

      // App.globalData.code = res.code

      if (res.code) {

      //发起网络请求

      // console.log(res.code)

      this.globalData.code=res.code


      } else {

      // console.log('获取用户登录态失败!' + res.errMsg)

      }

      },

      })

      //获取用户手机号,token

      wx.getStorage({

      key: 'UserData',

      success: res=>{

      // console.log(res);

      this.globalData.mobile=res.data.mobile;

      this.globalData.UserToken = res.data.token;

      // console.log(this.globalData.mobile);

      // console.log(this.globalData.UserToken);

      if (this.globalData.UserToken == undefined || this.globalData.UserToken == '') {

      this.globalData.isLogin=false

      } else if (this.globalData.UserToken != undefined && this.globalData.UserToken != '') {

      this.globalData.isLogin=true

      }

      }

      })

      },

      })


      2018-09-15
      回复
    • AAA000-程序员的可乐
      AAA000-程序员的可乐
      2018-09-15回复AAA000-程序员的可乐

      麻烦帮忙看一下

      2018-09-15
      回复
    • 🚗🚕🚙🚌🚎🚒🚐🚚
      🚗🚕🚙🚌🚎🚒🚐🚚
      2018-09-17回复AAA000-程序员的可乐
      page.onLoad();这句有问题,onShow后为啥还有执行一边onLoad,而且这个没带参数options
      2018-09-17
      回复
    • AAA000-程序员的可乐
      AAA000-程序员的可乐
      2018-09-18

      我就是想,对当前页面进行一些JS操作 ,重新加载刷新当前页,除了这种方法,还有其他方法嘛?

      2018-09-18
      回复
  • AAA000-程序员的可乐
    AAA000-程序员的可乐
    2018-09-14

    我在跳转到当前页面前,是传了参数的,有没有办法屏蔽这种错误

    2018-09-14
    赞同
    回复