Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2+ http.get() stuck in pending status

I have a MEAN app. Angular CLI: 7.1.4 Node: 10.1.0 OS: win32 x64 Angular: 7.1.4

recently the http requests from HttpClientModule have been getting stuck and not posting to the node server: Img of chrome dev tools xhr pending request

The nodejs server ( running locally and in production (azure web app) does not indicate that it ever received the request. This happens inconsistently. some times it completes other times it just hangs. Here is a snippet of a testConnection call from Angular to the server:

Angular service

import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { environment } from '../../environments/environment';
const Headers: any = { withCredentials: true, responseType: 'json', headers: { 'Content-Type': 'application/json' } };

@Injectable({
  providedIn: 'root',
})
export class UserService {
  constructor(private _http: HttpClient) {}
    loginStatus() {
      return this._http.get(`${environment.serverURL}/api/login-status`, Headers).pipe(catchError(this.handleError));
    }}

Angular Component:

ngOnInit() {
this._userSvc.loginStatus().subscribe(
(result:any)=>{console.log(result)},
(error:any)=>{console.log(error)})
}

Node/express:

router.get('/login-status', (req, res, next) => {    
  if (req.isAuthenticated()) {
    res.status(200).json(req.user);
  } else {
    res.status(403).json({
      success: false,
      error: 'User not Authenticated',
      message: "Please return to the login in page and try again."
    })
  }
})

Node is using passport to authenticate

Don't get tied up with the passport issue because its not always this route that fails. I have simple routes that do no validation and just return some text that fail too.

I tried modifying my CORS options but I've only managed to block myself. There are times when restarting the server will allow the request to finish but not always.

like image 772
Meisterunner Avatar asked Oct 27 '22 19:10

Meisterunner


1 Answers

I found the problem and i'm embarrassed to say it was in the SQL connection string for mssql. I had ((config,error)=>{}) instead of the correct (config,(err)=>{}); This was in the de-serialize user function of passport. Nothing like looking over thousands of lines of code to spot one little problem.

like image 191
Meisterunner Avatar answered Nov 11 '22 13:11

Meisterunner