每当我使用near-cli
调用我的contract view
方法时,它都工作得很好,终端以JSON格式正确地输出结果。
但是当我在我的angular项目中使用near-api-js
方法调用相同的方法时,它给出了一个错误:
Error: Uncaught (in promise): TypeError: JSON.stringify cannot serialize cyclic structures.
near-cli
的输出以供参考,以及通过near-api-js
调用相同方法时的预期输出
{
files: [
{
owner: 'some string',
key: 'some string',
cid: 'some string'
}
],
length: 1
}
这可能是什么原因,解决方案是什么?
发布于 2021-07-22 04:47:49
循环引用示例:
var circularReference = {otherData: 123};
circularReference.myself = circularReference;
JSON.stringify(circularReference);
说明:circularReference
通过cirularReference.myself
引用自身。
Mozilla's website有一个很好的例子来说明如何找到和删除循环引用:
const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
JSON.stringify(circularReference, getCircularReplacer());
// {"otherData":123}
您可以修改循环引用,而不是删除循环引用。
然而,这是一种对症治疗。最好的办法是首先找出为什么会出现循环引用,如果这是由bug引起的,那么就修复它。
https://stackoverflow.com/questions/68476117
复制相似问题