angular - Angular 中可观察到的数据未定义
问题描述
我已经使用节点创建了一个 API,该 API 验证用户是否有效,然后为经过验证的用户创建一个有效令牌并将其传递,否则返回 401,但在角度部分我总是未定义。
// used to authenticate the user and create the token
app.post("/auth", (req, res) => {
if(req.body.email==="swarup@gmail.com" && req.body.pwd==="123456"){
var token = jwt.sign({email: "user.email"}, 'hubDemo', {expiresIn: '1h'});
res.send({token})
}else{
res.send(401)
}
我有一个在 Angular 中调用这个 API 的服务:
return this.http.post<{token: string}>("/api/auth",{email:email,pwd:pwd})
我在一个组件中订阅它
import { Component, OnInit } from '@angular/core';
import {GetvalidationService} from '../_services/getvalidation.service'
import { Router } from '@angular/router';
import { Store } from '@ngrx/store';
import { Appstate } from '../app.state';
import { loginEmail } from './../model/models.tut'
import * as LoginActions from '../action/action.tut';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
email: string;
password: string;
data;
public error: string;
constructor(private validationService: GetvalidationService, private router: Router, private store: Store<Appstate>) { }
ngOnInit(): void {
}
onSubmit(){
var a = this.validationService.authenticateLogin(this.email,this.password).subscribe((data)=>this.data=data);
var b =this.error;
var c= this.data;
//store the email of logged in user in store
this.store.dispatch(new LoginActions.AddLogin({name: this.email}) )
this.router.navigate(['content'])
}
}