javascript - n.parentNode.removeChild 表达式中的 "null is not an object"

标签 javascript exception web-applications sentry

问题: 我从生产网页收到以下错误报告:

TypeErrores6-shim/es6-shim in process
null is not an object (evaluating 'n.parentNode.removeChild')

问题

从低层面来看,最有可能出现的情况是什么?

  1. nnull(无论 n 是什么)
  2. n.parentNodenull
  3. argn.parentNode.removeChild(arg) 中为 null

更多详细信息(如果相关):

我们使用 Sentry 错误报告系统来报告错误。

此错误发生在 Mobile Safari 和 Chrome Mobile 下。

源映射似乎已损坏,所以我不知道错误指的是 es6-shim 脚本的哪一行(很可能根本就是 es6-shim 问题,因为 es6-shim 代码中没有出现 removeChild )。

最佳答案

您可以轻松设置所有三种情况来确定它是哪一种 ( also on jsFiddle ):

var n, p;

console.log("If n were null:")
try {
  n = null;
  n.parentNode.removeChild(null);
} catch (e1) {
  console.log(e1.message);
}

console.log("If parentNode were null:")
try {
  n = document.createElement("div");
  n.parentNode.removeChild(null);
} catch (e2) {
  console.log(e2.message);
}

console.log("If arg were null:")
try {
  p = document.createElement("div");
  n = document.createElement("div");
  p.appendChild(n);
  n.parentNode.removeChild(null);
} catch (e3) {
  console.log(e3.message);
}
.as-console-wrapper {
  max-height: 100% !important;
}

当我在移动 Chrome 上运行该程序时,我看到:

If n were null:
null is not an object (evaluating 'n.parentNode')
If parentNode were null:
null is not an object (evaluating 'n.parentNode.removeChild')
If arg were null:
Argument 1 ('child') to Node.removeChild must be an instance of Node

因此,似乎正在对 n 引用的对象调用 n.parentNode.removeChild(...),其中 parentNodenull

虽然在es6-shim中报错,但不太可能是es6-shim本身的错误(正如你所说,es6-shim中没有removeNode);它更有可能是从提供给 es6-shim 函数之一的回调中抛出的。

关于javascript - n.parentNode.removeChild 表达式中的 "null is not an object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48616055/

相关文章:

php - 单选按钮验证器

javascript - 无法读取Angular2模板函数中的回调函数

java - 我的类(class)出现空指针错误

javascript - 在 VueJS 组件中引用静态资源

javascript - 移动设备中带箭头的水平菜单栏

javascript - 通过动态生成的href标签调用javascript函数(js函数只有一个参数)

.net - 如何在 Application.ThreadException 事件处理程序中获取整个异常链?

Android:requestLocationUpdates 抛出异常

javascript - 将第三方视频集成到 Web 应用程序中

node.js - 如何通过 SSH 将 Rocket Chat 安装到服务器?