BehaviorSubject, Observable, Having trouble understanding and need little assistance

55 views
Skip to first unread message

learningA

unread,
Mar 30, 2020, 10:06:20 AM3/30/20
to Angular and AngularJS discussion
Hello,

I been trying to understand the topic of BehaviorSubject & Observable by using a simple login application with no success and I'm wondering if someone can look at the code and let me know what aim doing wrong. I been at it for a month with no success.

Angular project


what i have in my services

 loginIn() {   
        this._http.get<Login[]>(this._url) 
            .map(data => this.members == data) \\does work
            .subscribe(data => { this.dataStore.next(data);
           console.log("loginIn:=> "+ this.dataStore);
           this._route.navigate(['/home']);
        }
      );
     
           
    }


app

Arnaud Deman

unread,
Mar 30, 2020, 12:02:04 PM3/30/20
to Angular and AngularJS discussion
Hello,

iin loginIn you do not return anything. And you already subscribe to the observable.
So in the app you try to call subscribe to an empty type. I would use something more like this which returns an observable:
to log in() {
return this._http.get <Login []> (this._url) .pipe (
tap (data => {this.dataStore.next (data);
console.log ("loginIn: =>" + this.dataStore);
this._route.navigate (['/ home'])
})
);
}

There are other errors but I think that they are due to the model: that is to say: _url is not a URL but a json object (incorrect).

I'm not sure if this is the best place to call route._navigate, either move them to the final subscriptions, or better use a route guard:
https://angular.io/guide/router#canactivate-requiring-authentication

I hope this will help you,
Regards,
Arnaud.

Bb Mattieu

unread,
Mar 30, 2020, 12:09:59 PM3/30/20
to ang...@googlegroups.com
Check out Full Stack Master on youtube. Search for Behavior Subject on the channel. He has a very good explanation for it. It will help. Good luck.

--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/a693a05e-90e3-4405-a38f-2c134964bfb8%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages