I'm building an app in angular 9 and I have a .json
file to get data and I want to get first element.
I tried it from take(1)
, first
and single()
but none worked.
How can we fetch data from JSON
file?
JSON
[
{
"username": "test",
"password": "test",
"firstName": "User",
"lastName": "Test"
},
{
"username": "test1",
"password": "test1",
"firstName": "User1",
"lastName": "Test1"
}
]
Component.ts
this.authenticationService.login()
.pipe(first())
.subscribe(
data => {
console.log(data)
if(this.loginModel.username == data.username && this.loginModel.password == data.password){
localStorage.setItem('currentUser', JSON.stringify(data));
this.router.navigate([this.returnUrl]);
}
else{
// this.error = error;
// this.loading = false;
alert('Please enter correct USERNAME OR PASSWORD');
}
},
error => {
this.error = error;
this.loading = false;
}
);