node.js - 如何在角度 6 中修复未定义
问题描述
我正在开发一个 API 来注册用户,在成功注册后应该将他们引导到管理仪表板,但是我得到这个弹出窗口告诉我 localhost:4200 说未定义,尝试了几种解决方案,但仍然卡住,但所有后端工作邮递员很好
这是我的代码
这是我的 login.component.ts
import { Component, OnInit } from '@angular/core';
import { AuthService } from 'src/app/auth.service';
import { Router } from '@angular/router';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor(private Auth: AuthService,private router: Router) { }
ngOnInit() {
}
loginUser(event)
{
event.preventDefault()
const target = event.target
const email= target.querySelector('#email').value
const password = target.querySelector('#password').value
this.Auth.getUserDetails(email, password).subscribe(data => {
if(data.success)
{
//redirect the person to admin page
this.router.navigate(['admindashboard'])
this.Auth.setLoggedIn(true)
}
else
{
window.alert(data.message);
}
});
console.log(email, password)
}
}
这是 auth.service.ts
import { Injectable } from '@angular/core';
import{ HttpClient } from '@angular/common/http';
interface myData
{
success:boolean,
message: string
}
@Injectable({
providedIn: 'root'
})
export class AuthService {
uri : String = 'http://localhost:4000';
private loggedInStatus = false
constructor(private http: HttpClient) { }
setLoggedIn(value: boolean)
{
this.loggedInStatus = value
}
get isLoggedIn()
{
return this.loggedInStatus
}
getUserDetails(email: String,password:String){
//post these details to the database
return this.http.post<myData>(`${this.uri}/auth`,{ email,password });
}
}
在 Chrome 上我得到一个弹出窗口说 localhost:4200 undefined
iat: 1571218802, exp: 1571218862, token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1Z…g2Mn0.8KN8c_K0XdGno24tKwysZaBpM6PcGlXMs3foWHhVnac"}exp: 1571218862iat: 1571218802token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZGE2Yzg0Mzk5NTJkZTA3ZTM2NDVhZjIiLCJlbWFpbCI6Imp1bWFqdW1hQGdtYWlsLmNvbSIsInBhc3N3b3JkIjoiJDJhJDEwJHFrRm85WktDRWRCaHZkTnFpOVA1V2VIT2FuSXJ4MXVrcWN1dWFDYzMvR3lLQ1poaVpLek02IiwiX192IjowLCJpYXQiOjE1NzEyMTg4MDIsImV4cCI6MTU3MTIxODg2Mn0.8KN8c_K0XdGno24tKwysZaBpM6PcGlXMs3foWHhVnac" proto : constructor: ƒ Object()hasOwnProperty: ƒ hasOwnProperty()isPrototypeOf: ƒ isPrototypeOf()propertyIsEnumerable: ƒ propertyIsEnumerable()toLocaleString: ƒ toLocaleString()toString: ƒ ()valueOf: ƒ valueOf() defineGetter : ƒ defineGetter ()defineSetter : ƒ defineSetter () lookupGetter : ƒ lookupGetter () lookupSetter : ƒ lookupSetter ()get proto : ƒ proto ()set proto : ƒ proto ()arguments: (...)caller: (...)length: 1name: “设置原型”原型:ƒ ()[[Scopes]]: Scopes[0]
解决方案
推荐阅读
- react-native - 仅当我不使用调试器时应用程序崩溃。无法克服的局面。我应该怎么办?
- ruby-on-rails - Rails Websocket 和不同的实现
- go - 看不懂5.6.1。警告:捕获迭代变量
- java - 在 JTextPane 中粘贴时如何保留源文本样式?
- hive - 使用 hive 计算在一个状态下花费的时间
- c - C语言中的3D Sobel算子算法
- python - Pandas 从 CSV 生成多个 xlsx 文件
- c - Linux 等待队列...排序重要吗?
- python - 在熊猫中查找一个月的第一天和最后一天
- python - pandas python - 如何将数组(与数据框长度不同)转换为数据框并保留行名和列名?