I am a beginner in Angular and I'm using this method to allow users to log into the system.
loginuser(){
const user = {
username: this.username,
password: this.password
};
this.Auth.loginUser(user).subscribe((res)=>{
if(res){
this.Auth.storeData(res.token, res.user);
this.flashMessage.show('You are Successfully logged In.', { cssClass: 'alert-success', timeout: 5000 });
this.router.navigate(['/userProfile']);
}else{
this.flashMessage.show('Your Password or Username is Incorrect.', { cssClass: 'alert-success', timeout: 5000 });
this.router.navigate(['/login']);
}
});
}
However, I encountered an error in my VScode editor which looks like this. https://i.sstatic.net/dNt45.png
Additionally, my angular-cli is showing these errors as well. https://i.sstatic.net/08pvq.png I have clearly defined those parameters in my node function. Here is my node function:
router.post('/login', (req, res)=>{
const name = req.body.username;
const password = req.body.password;
//console.log(password);
User.findByName(name,(err, user)=>{
if(err) throw err;
if(!user){
res.json({state: false, msg:"No User Found"});
//return false;
}
User.passwordCheck(password,user.password, (err, match)=>{
if(err) throw err;
if(match){
const token = jwt.sign({User} , config.secret, {expiresIn:86400*3});
res.json({
state: true,
token: "JWT" + token,
user:{
id:user._id,
name:user.name,
ID:user.ID,
IDexpDate: user.IDexpDate
}
});
}else {
res.json({state:false,msg:"password does not match"});
}
});
});
console.log(password);
});
I am using Angular version 6 for developing this application. I have searched extensively but couldn't find any similar issues. It's possible that my service functions may also need review, so here are my service files:
readonly baseURL ='http://localhost:3000/users/login';
storeData(token,userdata){
localStorage.setItem("tokenid",token);
localStorage.setItem("user",JSON.stringify(userdata));
this.authtoken = token;
this.user= userdata
}
loginUser(user){
let header = new HttpHeaders();
header.append('Content-Type','application/json');
return this.http.post(this.baseURL,user,{headers:header});
}
When I console.log res.token and res.user, it gives me values in my console log. My concern is why the VScode editor is showing me errors like that. Thank you very much for taking the time to help me with this.