Node.js 角度:登录页面不工作,用户名和密码正确,但仍显示不正确的详细信息?
我正在用NodeJS API和Angular创建一个登录页面,用于登录的NodeJS API是Node.js 角度:登录页面不工作,用户名和密码正确,但仍显示不正确的详细信息?,node.js,angular,authentication,mean-stack,Node.js,Angular,Authentication,Mean Stack,我正在用NodeJS API和Angular创建一个登录页面,用于登录的NodeJS API是localhost:3000/用户,每当我尝试登录时,它会显示错误的用户名和密码word,甚至我的密码和电子邮件也会被更正 login.component.TS import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { AppServiceService }
localhost:3000/用户
,每当我尝试登录时,它会显示错误的用户名和密码word,甚至我的密码和电子邮件也会被更正
login.component.TS
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AppServiceService } from './../app-service.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor(private auth : AppServiceService, private _router: Router) { }
public loginuserdata = { email: '', password:'' };
ngOnInit() {
var self = this;
if (localStorage.hasOwnProperty("storageselectedclient") && (localStorage.getItem("storageselectedclient") !="")) {
window.location.replace(location.origin+"/home");
} else{
localStorage.clear();
}
var input = document.getElementsByTagName("body")[0];
// Execute a function when the user releases a key on the keyboard
input.addEventListener("keyup", function(event) {
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
self.loginuser();
// Cancel the default action, if needed
event.preventDefault();
// Trigger the button element with a click
// document.getElementsByClassName("loginbtn").click();
}
});}
loginuser(){
this.auth.login(this.loginuserdata)
.subscribe(
res=>{
if(res.code=="204"){
alert("usename and passsword is incorrect");
}else{
localStorage.setItem('token', res.token)
localStorage.setItem('email', res.result.replace(/\s/g, ""))
this._router.navigate(['/home']);
}
},
err => {
console.log(err);
}
)
}
}
HTML
<form class="contact-form row">
<div class="form-field col-lg-12">
<input id="name" [(ngModel)]="loginuserdata.email" class="input-text js-input" type="text" placeholder="Email" name="email" required="">
</div>
<div class="form-field col-lg-12 ">
<input type="password" id="company" required [(ngModel)]="loginuserdata.password" class="input-text js-input" placeholder="Password" required="" [ngModelOptions]="{standalone: true}">
</div>
<div class="form-field col-lg-12">
<input class="submit-btn w-100" routerLink="/home/deletearticle" (click)="loginuser()" value="Submit">
</div>
我已经在Postman中尝试了我的API调用,它工作正常您可以在这行代码中确认
loginuser(){this.auth.login(this.loginuserdata).subscribe(res=>{
here…this.loginuserdata
和res
是否有值?如何确定值是否存在?我的意思是在调用loginUser()时您正在传递的函数this.loginuserdata
,当您传递给auth services的login方法时,该函数的值是什么?它在我的代码中拼写错误,避免直接访问DOM,请使用@ViewChild()或模板引用变量。。
import { Injectable } from '@angular/core';
import { HttpClient} from '@angular/common/http';
import { AppSetting } from './appsetting'
@Injectable({
providedIn: 'root'
})
export class AppServiceService {
private SERVERURL = AppSetting.API;
constructor(private http: HttpClient) { }
login(user){
console.log(user);
return this.http.post<any>(this.SERVERURL+"users",user);
}
}
export class AppSetting{
public static API='http://localhost:3000/';
}