How to make a sequence of http requests in Angular 6 using RxJS

For calls that depend on previous result you should use concatMap firstPOSTCallToAPI(‘url’, data).pipe( concatMap(result1 => secondPOSTCallToAPI(‘url’, result1)) concatMap( result2 => thirdPOSTCallToAPI(‘url’, result2)) concatMap(result3 => fourthPOSTCallToAPI(‘url’, result3)) …. ).subscribe( success => { /* display success msg */ }, errorData => { /* display error msg */ } ); if your async method does not depend on … Read more

Angular 2 Basic Authentication not working

Simplified version to add custom headers to your request: import {Injectable} from ‘@angular/core’; import {Http, Headers} from ‘@angular/http’; @Injectable() export class ApiService { constructor(private _http: Http) {} call(url): Observable<any> { let username: string = ‘username’; let password: string = ‘password’; let headers: Headers = new Headers(); headers.append(“Authorization”, “Basic ” + btoa(username + “:” + password)); … Read more

How to use Dependency Injection (DI) correctly in Angular2?

Broad question, TL;DR version @Injectable() is a decorator which tells the typescript that decorated class has dependencies and does not mean that this class can be injected in some other. And then TypeScript understands that it needs to Inject the required metadata into decorated class when constructing, by using the imported dependencies. bootstrap(app, [service]) bootstrap() … Read more

TS2531: Object is possibly ‘null’

files is defined to be FileList | null so it can be null. You should either check for null (using an if) or use a “Non-null assertion operator” (!) if you are sure it is not null: if(nativeElement.files != null) { this.photoService.upload(this.vehicleId, nativeElement.files[0]) .subscribe(x => console.log(x)); } //OR this.photoService.upload(this.vehicleId, nativeElement.files![0]) .subscribe(x => console.log(x)); Note: The … Read more

formGroup expects a FormGroup instance

There are a few issues in your code <div [formGroup]=”form”> outside of a <form> tag <form [formGroup]=”form”> but the name of the property containing the FormGroup is loginForm therefore it should be <form [formGroup]=”loginForm”> [formControlName]=”dob” which passes the value of the property dob which doesn’t exist. What you need is to pass the string dob … Read more

How to make an http call every 2 minutes with RXJS?

Since you are already using Observables, simply make full use of it 🙂 Obersvable.interval() is your good friend here: In your component, do this: Observable .interval(2*60*1000) .timeInterval() .mergeMap(() => this.notificationService.getNotifications(this.token)) .subscribe(data => { console.log(data); }); Explanation: .interval() creates an observable that emits an event every 2 minutes. .timeInterval() convert an Observable that emits items into … Read more

angular4 httpclient csrf does not send x-xsrf-token

What you are looking for is HttpClientXsrfModule. Please read more about it here: https://angular.io/api/common/http/HttpClientXsrfModule. Your usage should be like this: imports: [ HttpClientModule, HttpClientXsrfModule.withOptions({ cookieName: ‘My-Xsrf-Cookie’, // this is optional headerName: ‘My-Xsrf-Header’ // this is optional }) ] Additionally, if your code targets API via absolute URL, default CSRF interceptor will not work out of … Read more

How to trace routing in Angular 2?

You can pass in a second argument with options: imports: [ RouterModule.forRoot( routes, { enableTracing: true } // <– debugging purposes only ) ] Angular will then log all events to the browser’s console, per the documentation: enableTracing?: boolean When true, log all internal navigation events to the console. Use for debugging.