首页 > 解决方案 > 如何在角度 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]

标签: node.jsangularmongoose

解决方案


推荐阅读