1

What does ERROR [NetworkError] mean?

I an using ng-universal to have a Nestjs backend and an Angular frontend. I am running the fullstack app with "npm run dev:ssr". Whenever I refresh the frontend page in my browser, the server terminal shows a new line output of "ERROR [NetworkError]" in red. When I make any network requests, it doesn't make that error, just when I make a request to the localhost port that is serving the app.

Jeremy Scott Peters
  • 377
  • 1
  • 3
  • 15
  • Some of you API calls failing maybe? Are you using an absolute url for these calls? – David Mar 12 '20 at 16:23
  • No, it isn't because of any API calls except the one static API that delivers the app. When I make any test API calls from Postman or make calls from the Frontend application, no new "ERROR [NetworkError]" errors appear but when I refresh the page a new one comes up. No absolute URL calls are made. They are all made relative to the location where the calls are made and the app is served which is localhost:4200. – Jeremy Scott Peters Mar 13 '20 at 21:03
  • Normally all API calls should be absolute when using angular universal. 4200 is not the port for ssr by default (I think its 4000). – David Mar 14 '20 at 06:59
  • It is the port for Angular apps and even when you add Nestjs with angular universal. – Jeremy Scott Peters Mar 15 '20 at 16:06

1 Answers1

0

If the api request is made to the server OnInit when reloading check isPlatformBrowser first when using ng-universal example.

import { Component, OnInit, PLATFORM_ID, Inject } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { HttpClient, HttpHeaders } from '@angular/common/http';

public testBrowser: boolean;
export class HomeComponent implements OnInit {

  public testBrowser  : boolean;
  public data         : any;
  
  constructor(private http: HttpClient, @Inject(PLATFORM_ID) platformId: string) {
    this.testBrowser = isPlatformBrowser(platformId);
  }

  ngOnInit() {
    if (this.testBrowser) {
      //avoid server NETWORK error
      this.data = this.http.get('/api');
    }
  }
}

I was getting this same error trying to make server calls from the client before checking isPlatformBrowser === true first OnInit and this solved my problem. Hopefully this can help this bug.

For reference this answer helped me squash this long standing bug. https://stackoverflow.com/a/46893433/4684183

Ian Poston Framer
  • 938
  • 12
  • 16
  • I did that and then it started giving me different errors. Thanks @Ian Poston Framer. but I just redid the app with different code and it worked fine with the new code. I think it had to do with the NestJS code but I don't know. – Jeremy Scott Peters Aug 04 '20 at 22:43