According to the console, I am facing an issue while trying to route to the dashboard after logging in because the surname property is undefined. However, when I check my alerts, I can see that it is filled correctly at all times.
login(surName: string, passWord){
const name = this.members.find( x => x.surname === surName)
const pass = this.members.find( x => x.password === passWord)
if(name && pass){
for (const element of this.members) {
if(element.surname === surName){
alert(element.surname + surName);
this.persist.setPersistence(surName, element.color);
alert("You are Logged in" + element.surname + element.color);
this.router.navigate(['/dashbaord']);
return;
}
}
}else{
alert("User Details are incorrect please make sure your username and password is correct");
}
}
I have checked from the point where data is being entered to when the login button is pressed and the values being passed into the set function. This is where the error occurs despite me being able to see the value, so I am puzzled about what might be happening
import { Injectable } from '@angular/core';
import { Persistance} from '../Persistance/persistance.model'
@Injectable({
providedIn: 'root'
})
export class PersistService {
persist: Persistance;
constructor() { }
setPersistence(surname : string, color: string){
alert(surname);
this.persist.surname = surname;
this.persist.color = color;
}
}
The Error:
core.js:4197 ERROR TypeError: Cannot set property 'surname' of undefined
Although I can observe the property being defined at all points where it's assigned or used, I'm still unable to understand why this error is occurring