忽略BEFORE_APP_SERIALIZED异常:TypeError:将圆形结构转换为JSON

时间:2019-09-30 16:51:23

标签: json angular server-side-rendering universal angular-transfer-state

我有一个使用Angular Universal进行服务器端渲染的Angular应用程序,以及TransferState模块将应用程序的状态从服务器传递到客户端。

我在某些页面上收到此警告日志:

Ignoring BEFORE_APP_SERIALIZED Exception:  TypeError: Converting circular structure to JSON
at Object.stringify (<anonymous>)
at TransferState.toJson (/Users/my-user/Documents/MyApp/my-app/dist/server.js:40450:21)
at /Users/my-user/Documents/MyApp/my-app/dist/server.js:1338:122
at /Users/my-user/Documents/MyApp/my-app/dist/server.js:1399:29
at ZoneDelegate.invoke (/Users/my-user/Documents/MyApp/my-app/dist/server.js:167690:26)
at Zone.run (/Users/my-user/Documents/MyApp/my-app/dist/server.js:167449:43)
at /Users/my-user/Documents/MyApp/my-app/dist/server.js:168188:34
at ZoneDelegate.invokeTask (/Users/my-user/Documents/MyApp/my-app/dist/server.js:167722:31)
at Zone.runTask (/Users/my-user/Documents/MyApp/my-app/dist/server.js:167494:47)
at drainMicroTaskQueue (/Users/my-user/Documents/MyApp/my-app/dist/server.js:167900:35)

我不太担心警告本身(尽管我想知道到底是什么引起了该循环引用),但是我发现它每次都记录警告,而不管环境如何,这实在令人讨厌(ergo还在生产中)。

我如何a)防止该错误? b)防止警告日志?

Here是日志的来源:

function _render<T>(
    platform: PlatformRef, moduleRefPromise: Promise<NgModuleRef<T>>): Promise<string> {
  return moduleRefPromise.then((moduleRef) => {
    const transitionId = moduleRef.injector.get(ɵTRANSITION_ID, null);
    if (!transitionId) {
      throw new Error(
          `renderModule[Factory]() requires the use of BrowserModule.withServerTransition() to ensure
the server-rendered app can be properly bootstrapped into a client app.`);
    }
    const applicationRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
    return applicationRef.isStable.pipe((first((isStable: boolean) => isStable)))
        .toPromise()
        .then(() => {
          const platformState = platform.injector.get(PlatformState);

          const asyncPromises: Promise<any>[] = [];

          // Run any BEFORE_APP_SERIALIZED callbacks just before rendering to string.
          const callbacks = moduleRef.injector.get(BEFORE_APP_SERIALIZED, null);
          if (callbacks) {
            for (const callback of callbacks) {
              try {
                const callbackResult = callback();
                if (ɵisPromise(callbackResult)) {
                  asyncPromises.push(callbackResult);
                }

              } catch (e) {
                // Ignore exceptions.
                console.warn('Ignoring BEFORE_APP_SERIALIZED Exception: ', e);
              }
            }
          }

          const complete = () => {
            const output = platformState.renderToString();
            platform.destroy();
            return output;
          };

          if (asyncPromises.length === 0) {
            return complete();
          }

          return Promise
              .all(asyncPromises.map(asyncPromise => {
                return asyncPromise.catch(
                    e => { console.warn('Ignoring BEFORE_APP_SERIALIZED Exception: ', e); });
              }))
              .then(complete);
        });
  });
}

0 个答案:

没有答案