TypeError: Converting circular structure to JSON when trying to POST request

前端 未结 6 1034
星月不相逢
星月不相逢 2021-02-12 15:34

I am getting the error in the title and here is the full stack trace, i am not sure what it is, any insight is welcomed!

browser_adapter.js:84 EXCEPTION: Error i         


        
6条回答
  •  我寻月下人不归
    2021-02-12 16:34

    This code will fail for circular reference:

    JSON.stringify(circularReference);
    

    // TypeError: cyclic object value

    Use the below code:

        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());
    

提交回复
热议问题