问题: 我从生产网页收到以下错误报告:
TypeErrores6-shim/es6-shim in process
null is not an object (evaluating 'n.parentNode.removeChild')
问题
从低层面来看,最有可能出现的情况是什么?
n
为null
(无论n
是什么)n.parentNode
为null
arg
在n.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(...)
,其中 parentNode
为null
。
虽然在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/