Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

快手小程序事件报错n.forEach is not a function #5112

Open
jusong-cmd opened this issue Aug 27, 2024 · 1 comment
Open

快手小程序事件报错n.forEach is not a function #5112

jusong-cmd opened this issue Aug 27, 2024 · 1 comment
Labels

Comments

@jusong-cmd
Copy link

小程序编译后的JS vender.js
rget,e.detail)),e}(e);var r=(e.currentTarget||e.target).dataset;if(!r)return console.warn("事件信息不存在");var n=r.eventOpts||r["event-opts"];if(!n)return console.warn("事件信息不存在");var i=e.type,o=[];return n.forEach((function(r){var n=r[0],a=r[1],c="^"===n.charAt(0);n=c?n.slice(1):n;var u="~"===n.charAt(0);n=u?n.slice(1):n,a&&function(e,t){return e===t||"regionchange"===t&&("begin"===e||"end"===e)}

未混淆的代码 vender.js
`function handleEvent(event) {
var _this3 = this;
event = wrapper$1(event);

// [['tap',[['handle',[1,2,a]],['handle1',[1,2,a]]]]]
var dataset = (event.currentTarget || event.target).dataset;
if (!dataset) {
return console.warn('事件信息不存在');
}
var eventOpts = dataset.eventOpts || dataset['event-opts']; // 支付宝 web-view 组件 dataset 非驼峰
if (!eventOpts) {
return console.warn('事件信息不存在');
}

// [['handle',[1,2,a]],['handle1',[1,2,a]]]
var eventType = event.type;
var ret = [];
eventOpts.forEach(function (eventOpt) {
var type = eventOpt[0];
var eventsArray = eventOpt[1];
var isCustom = type.charAt(0) === CUSTOM;
type = isCustom ? type.slice(1) : type;
var isOnce = type.charAt(0) === ONCE;
type = isOnce ? type.slice(1) : type;
if (eventsArray && isMatchEventType(eventType, type)) {
eventsArray.forEach(function (eventArray) {
var methodName = eventArray[0];
if (methodName) {
var handlerCtx = _this3.$vm;
if (handlerCtx.$options.generic) {
// mp-weixin,mp-toutiao 抽象节点模拟 scoped slots
handlerCtx = getContextVm(handlerCtx) || handlerCtx;
}
if (methodName === '$emit') {
handlerCtx.$emit.apply(handlerCtx, processEventArgs(_this3.$vm, event, eventArray[1], eventArray[2], isCustom, methodName));
return;
}
var handler = handlerCtx[methodName];
if (!isFn(handler)) {
var _type = _this3.$vm.mpType === 'page' ? 'Page' : 'Component';
var path = _this3.route || _this3.is;
throw new Error("".concat(_type, " "").concat(path, "" does not have a method "").concat(methodName, """));
}
if (isOnce) {
if (handler.once) {
return;
}
handler.once = true;
}
var params = processEventArgs(_this3.$vm, event, eventArray[1], eventArray[2], isCustom, methodName);
params = Array.isArray(params) ? params : [];
// 参数尾部增加原始事件对象用于复杂表达式内获取额外数据
if (/=\s*\S+.eventParams\s*||\s*\S+[['"]event-params['"]]/.test(handler.toString())) {
// eslint-disable-next-line no-sparse-arrays
params = params.concat([,,,,,,,,,, event]);
}
ret.push(handler.apply(handlerCtx, params));
}
});
}
});
if (eventType === 'input' && ret.length === 1 && typeof ret[0] !== 'undefined') {
return ret[0];
}
}`
eventOpts 存在但不为数组,核心库里需要兼容一下eventOpts不为数组,每次编译后处理很麻烦

@jusong-cmd
Copy link
Author

这个是vender.js核心库,在快手正式版小程序中复现概率很大,也给快手开发反馈了
在快手小程序中特定概率eventOpts 存在但不为数组,核心库里需要兼容一下eventOpts不为数组
该问题不影响使用,但在小程序后台显示一堆异常
下面是核心库源码

function handleEvent(event) {
  var _this3 = this;
  event = wrapper$1(event);

  // [['tap',[['handle',[1,2,a]],['handle1',[1,2,a]]]]]
  var dataset = (event.currentTarget || event.target).dataset;
  if (!dataset) {
    return console.warn('事件信息不存在');
  }
  var eventOpts = dataset.eventOpts || dataset['event-opts']; // 支付宝 web-view 组件 dataset 非驼峰
  if (!eventOpts) {
    return console.warn('事件信息不存在');
  }

  // [['handle',[1,2,a]],['handle1',[1,2,a]]]
  var eventType = event.type;
  var ret = [];
  eventOpts.forEach(function (eventOpt) {
    var type = eventOpt[0];
    var eventsArray = eventOpt[1];
    var isCustom = type.charAt(0) === CUSTOM;
    type = isCustom ? type.slice(1) : type;
    var isOnce = type.charAt(0) === ONCE;
    type = isOnce ? type.slice(1) : type;
    if (eventsArray && isMatchEventType(eventType, type)) {
      eventsArray.forEach(function (eventArray) {
        var methodName = eventArray[0];
        if (methodName) {
          var handlerCtx = _this3.$vm;
          if (handlerCtx.$options.generic) {
            // mp-weixin,mp-toutiao 抽象节点模拟 scoped slots
            handlerCtx = getContextVm(handlerCtx) || handlerCtx;
          }
          if (methodName === '$emit') {
            handlerCtx.$emit.apply(handlerCtx, processEventArgs(_this3.$vm, event, eventArray[1], eventArray[2], isCustom, methodName));
            return;
          }
          var handler = handlerCtx[methodName];
          if (!isFn(handler)) {
            var _type = _this3.$vm.mpType === 'page' ? 'Page' : 'Component';
            var path = _this3.route || _this3.is;
            throw new Error("".concat(_type, " \"").concat(path, "\" does not have a method \"").concat(methodName, "\""));
          }
          if (isOnce) {
            if (handler.once) {
              return;
            }
            handler.once = true;
          }
          var params = processEventArgs(_this3.$vm, event, eventArray[1], eventArray[2], isCustom, methodName);
          params = Array.isArray(params) ? params : [];
          // 参数尾部增加原始事件对象用于复杂表达式内获取额外数据
          if (/=\s*\S+\.eventParams\s*\|\|\s*\S+\[['"]event-params['"]\]/.test(handler.toString())) {
            // eslint-disable-next-line no-sparse-arrays
            params = params.concat([,,,,,,,,,, event]);
          }
          ret.push(handler.apply(handlerCtx, params));
        }
      });
    }
  });
  if (eventType === 'input' && ret.length === 1 && typeof ret[0] !== 'undefined') {
    return ret[0];
  }
}

快手小程序编译后报错

错误信息:

n.forEach is not a function

错误路径:

common/vendor.js

错误代码:

rget,e.detail)),e}(e);var r=(e.currentTarget||e.target).dataset;if(!r)return console.warn("事件信息不存在");var n=r.eventOpts||r["event-opts"];if(!n)return console.warn("事件信息不存在");var i=e.type,o=[];return n.forEach((function(r){var n=r[0],a=r[1],c="^"===n.charAt(0);n=c?n.slice(1):n;var u="~"===n.charAt(0);n=u?n.slice(1):n,a&&function(e,t){return e===t||"regionchange"===t&&("begin"===e||"end"===e)}

核心库兼容一下,这样可以兼容一下快手小程序的问题

 if (!eventOpts||!Array.isArray(eventOpts)) {
    return console.warn('事件信息不存在');
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants
@bfc846958672 @jusong-cmd and others