Home > database >  About http.post cannot tigger
About http.post cannot tigger

Time:10-10

I'm writing a simple service in Angular, but it have not trigger inside can anyone advise :

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject } from 'rxjs';
import { map,  tap } from 'rxjs/operators';

import { User } from './user';
@Injectable({
  providedIn: 'root'
})
export class AuthService {
  
  private signinuser = new BehaviorSubject<User>(null);
  CurrentUser = this.signinuser.asObservable();
  
  constructor(private http: HttpClient) 
  {}

  signin (username : string, password :string) 
  {
    console.log(`Get username : ${username} - 1`);
    console.log(`Get password : ${password} - 2`);
    this.http.post<any>(`http://localhost:4000`, {"userid": username, "UserPassword": password})
        .pipe
        (
          tap( res => console.log('HTTP response here:', res)),
          map
          (
            
            data=>this.signinuser.next(data)
          ),
          tap(console.log)
         
        )
    this.signinuser.subscribe( getnewdata => { console.log(getnewdata) });
  }
  


}

when I call this.AuthService.signin ('test', 'password') from other component

The output result show this :
Get username : test - 1
Get username : password - 2
Test Signuser value : null

Thank you

CodePudding user response:

You have to subscribe to your post-call.

Else the call will not be executed and all the statements in pipe are not called. See the second to last line:
this.http.post<any>(`http://localhost:4000`, {"userid": username, "UserPassword": password})
    .pipe
    (
      tap( res => console.log('HTTP response here:', res)),
      map
      (
        
        data=>this.signinuser.next(data)
      ),
      tap(console.log)
     
    ).subscribe();
this.signinuser.subscribe( getnewdata => { console.log(getnewdata) });
  • Related