我正在尝试创建一个原型方法来获取对象中的嵌套属性(如果不存在则返回null)。
Object.prototype.getNested = function() {
var args = Array.prototype.slice.call(arguments);
var obj = this;
for (var i = 0; i < args.length; i++) {
if (!obj.hasOwnProperty(args[i])) return null;
obj = obj[args[i]];
}
return obj;
};
所以我收到了这个错误:
uncaughtException: Cowardly refusing to pack object with circular reference getNested
这个错误意味着什么?我以前从未见过这个。
答案 0 :(得分:1)
好的,我的猜测是这个错误消息来自msgpack npm module as indicated in the source code here。这意味着您正在尝试序列化引用自身的对象。这是我在节点REPL中重现这个错误:
> var msgpack = require("msgpack");
> var obj = {};
> obj["me"] = obj;
{ me: [Circular] }
> msgpack.pack(obj);
TypeError: Cowardly refusing to pack object with circular reference
at Object.pack (/Users/plyons/projects/stackoverflow/node_modules/msgpack/lib/msgpack.js:30:18)
at repl:1:9
at REPLServer.self.eval (repl.js:110:21)
at repl.js:249:20
at REPLServer.self.eval (repl.js:122:7)
at Interface.<anonymous> (repl.js:239:12)
at Interface.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
您需要调试该堆栈并查看哪个确切的对象传递给msgpack.pack
,然后您就可以了解真正的根本原因。可能只是将getNested
添加为属性时,请务必将其标记为不可枚举。