Skip to content

taro cli 1.2.22升级到1.2.26后小程序运行报错 Cannot read property 'id' of undefined #2792

Closed
@flyrui316

Description

问题描述
[问题描述:站在其它人的角度尽可能清晰地、简洁地把问题描述清楚]
taro cli 1.2.22升级到1.2.26后,原生小程序先运行taro convert,然后运行 npm run dev:weapp后,在原来小程序onLoad函数里面如果存在参数op,想获得op里面的值会报错,Cannot read property 'id' of undefined
复现步骤
[复现问题的步骤]
1.taro convert
2.npm run dev:weapp
3.运行编译后的代码

[或者可以直接贴源代码,能贴文字就不要截图]

// 这里可以贴代码
小程序源码
Page({
  data: {

  },
  onLoad: function (op) {
    let id = op.id
    console.log(id)
  }
})


taro convert后的代码:
import { Block } from '@tarojs/components'
import Taro from '@tarojs/taro'
import withWeapp from '@tarojs/with-weapp'

@withWeapp('Page')
class _C extends Taro.Component {
  state = {}
  componentWillMount = op => {
    let id = op.id
    console.log(id)
  }
  config = {}

  render() {
    return <Block>123</Block>
  }
}
/**
 * Created by ryan on 2019/4/16.
 */

export default _C


npm run dev:weapp后的代码
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

var _dec, _class, _class2, _temp2;

var _index = require("../../../npm/@tarojs/taro-weapp/index.js");

var _index2 = _interopRequireDefault(_index);

var _index3 = require("../../../npm/@tarojs/with-weapp/index.js");

var _index4 = _interopRequireDefault(_index3);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var _C = (_dec = (0, _index4.default)('Page'), _dec(_class = (_temp2 = _class2 = function (_Taro$Component) {
  _inherits(_C, _Taro$Component);

  function _C() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, _C);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = _C.__proto__ || Object.getPrototypeOf(_C)).call.apply(_ref, [this].concat(args))), _this), _this.$usedState = [], _this.state = {}, _this.componentWillMount = function (op) {
      var id = op.id;
      console.log(id);
    }, _this.config = {}, _this.$$refs = [], _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(_C, [{
    key: "_constructor",
    value: function _constructor(props) {
      _get(_C.prototype.__proto__ || Object.getPrototypeOf(_C.prototype), "_constructor", this).call(this, props);
    }
  }, {
    key: "_createData",
    value: function _createData() {
      this.__state = arguments[0] || this.state || {};
      this.__props = arguments[1] || this.props || {};
      var __isRunloopRef = arguments[2];
      ;
      Object.assign(this.__state, {});
      return this.__state;
    }
  }]);

  return _C;
}(_index2.default.Component), _class2.properties = {}, _class2.$$events = [], _temp2)) || _class);
/**
 * Created by ryan on 2019/4/16.
 */

exports.default = _C;

Component(require('../../../npm/@tarojs/taro-weapp/index.js').default.createComponent(_C, true));

期望行为
[这里请用简洁清晰的语言描述你期望的行为]
返回undefined,而不是报错
报错信息
thirdScriptError
Cannot read property 'id' of undefined;at "pages/pages/test/test" page lifeCycleMethod onLoad function
TypeError: Cannot read property 'id' of undefined
at BaseComponent._this.componentWillMount (http://127.0.0.1:41160/appservice/pages/pages/test/test.js:44:18)
at componentTrigger (http://127.0.0.1:41160/appservice/npm/@tarojs/taro-weapp/dist/index.js:3517:100)
at updateComponent (http://127.0.0.1:41160/appservice/npm/@tarojs/taro-weapp/dist/index.js:3755:5)
at H.initComponent (http://127.0.0.1:41160/appservice/npm/@tarojs/taro-weapp/dist/index.js:3559:3)
at H.weappComponentConf.methods.onLoad (http://127.0.0.1:41160/appservice/npm/@tarojs/taro-weapp/dist/index.js:3651:21)
at H. (http://127.0.0.1:41160/appservice/__dev__/WAService.js:1:983107)
at H.p.callPageLifeTime (http://127.0.0.1:41160/appservice/__dev__/WAService.js:1:982866)
at kt (http://127.0.0.1:41160/appservice/__dev__/WAService.js:1:998756)
at http://127.0.0.1:41160/appservice/__dev__/WAService.js:1:1001435
at It (http://127.0.0.1:41160/appservice/__dev__/WAService.js:1:1001
[这里请贴上你的完整报错截图或文字]

系统信息

Taro v1.2 及以上版本已添加 taro info 命令,方便大家查看系统及依赖信息,运行该命令后将结果贴下面即可。

Taro CLI 1.2.26 environment info:
System:
OS: macOS 10.14.4
Shell: 5.3 - /bin/zsh
Binaries:
Node: 8.10.0 - ~/.nvm/versions/node/v8.10.0/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.7.0 - ~/.nvm/versions/node/v8.10.0/bin/npm
npmGlobalPackages:
typescript: 3.1.6

  • 报错平台 [h5, weapp]
    weapp
    补充信息
    [可选]
    [根据你的调查研究,出现这个问题的原因可能在哪里?]

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions