0

I'm completely new to Angular, so I hope I can describe my problem clearly. I'm trying to get the date property from an observable to display it in KendoUI datetime picker in frontend.

Here is my source code: I have an object ResultObject with 2 properties (id, myCustomDate). The id property is of type number and the myCustomDate property is of type Date.

I got my data over a REST API request. Here is the call:

this.httpService.get<ResultObject>(this.service.Url + "getresultobject", { blockUi: true }).
    subscribe(result => this.result$.next(result));

//this is where I get the result through a web service call:
result$: Subject<ResultObject> = new BehaviorSubject<ResultObject>(null);

//this is where I create my observable, in the constructor:
this.resultObeservable$ = this.result$.map(r => r);

And here is the main problem which I have, I'm trying to assign a the property date from the object result:

this.resultObeservable$.subscribe(d => this.ngZone.run(() => {
   console.log(d);
   this.myCustomDate= d.resultDate; //----> here i get the exception
}));

The exception says that d is null or undefined.

As I said, I'm totally new to Angular and I even don't know if the usage of ngZone is correct like this. So it would be great if I get any help.

alaa_sayegh
  • 2,141
  • 4
  • 21
  • 37
  • I don't see any code which executes a HTTP request. Could you share that code with us? Are you sure you're still using Angular 2? 6 is the latest version. – David Walschots May 22 '18 at 13:54
  • If the initial value of the BehaviorSubject `result$` is `null`, you may prefer to use another kind of Subject. See [this question](https://stackoverflow.com/q/44693438/1009922). – ConnorsFan May 22 '18 at 13:57
  • @DavidWalschots, im using 5.2.0. I will correct it. The http code is only for the website call. I will add it anyway – alaa_sayegh May 22 '18 at 14:03
  • 1
    I'm not sure why you have these additional observables and subjects. Is there a reason you don't simply in `httpService.get.subscribe` write `this.myCustomDate = result.resultDate`? – David Walschots May 22 '18 at 14:07
  • Change `this.resultObeservable$ = this.result$.map(r => r);` to `this.resultObeservable$ = this.result$.asObservable()`. Does that work? – Lansana Camara May 22 '18 at 14:44

0 Answers0