Reputation: 75
I'm trying to check if the email and password in the login are in an array which I get through a function getUsers()
, but when I try to check it with find()
it says Property "find' does not exist on type 'Observable<any>*
. I don't know how to resolve it, I read that with pipe, but I don't really understand how it works.
On user.service.ts:
getUsers(){
return this.http.get<any>(this.usersUrl);
}
login(email:string, password:string):Observable<any>{
const users=this.getUsers();
return users.find(
(user:any)=> user.email === email && user.password ===password);
}
Upvotes: 0
Views: 3434
Reputation: 1430
You will have to subscribe to your observable if want to retrieve data inside of it.
in your case getUsers()
is returning an observable of users and you can subscribe to it inside your login()
function:
login(email: string, password: string): Observable<any> {
const users = this.users;
let loggedUser
// subscribe to the users observable
users.subscribe(usersList => {
// find the user
const userExists = usersList.find(user => user.email === email && user.password === password)
loggedUser = userExists
});
// return the found user as an observable, if nothing is found, the observable is gonna contain undefined
return of(loggedUser)
}
Here is a small demo to help you out: link
Upvotes: 1
Reputation: 116
Alternativeley you could convert the http.get to a promise and turn your login method into an async method:
public async login(email: string, password: string): Promise<YourUserType> {
const users = await this.getUsers().toPromise();
return users.find(({ email: eml, password: pw }) => eml === email && pw === password);
}
Upvotes: 3