简体   繁体   English

“对象”类型上不存在属性“管理员”

[英]Property 'admin' does not exist on type 'Object'

Ive been having this issue for a while now.我有这个问题有一段时间了。 The response is from my backend api and its saying this property dosent exist even though it exists.响应来自我的后端 api 并且它说这个属性即使存在也存在。 I have a total of like 18 errors in my angular application and it was working all fine until today and I dont know what to do to fix this "issue".我的 angular 应用程序中总共有 18 个错误,直到今天一切正常,我不知道如何解决这个“问题”。

import { Component, OnInit, Output } from '@angular/core';
import { Router } from '@angular/router';
import { TokenService } from '../../authentication/services/token.service';
import { HttpErrorResponse } from '@angular/common/http';
import { AdminService } from '../../authentication/services/admin.service';
import { CrudService } from '../services/crud.service';
import { map } from 'rxjs/operators';

@Component({
  selector: 'app-users',
  templateUrl: './users.component.html',
  styleUrls: ['./users.component.css'],
})
export class UsersComponent implements OnInit {
  constructor(
    private _token: TokenService,
    private _router: Router,
    private _admin: AdminService,
    private _crud: CrudService
  ) {}

  adminId: string;
  adminName: string;
  adminEmail: string;
  users: any[];
  deletedId: string;
  successMessage: string;
  p: number = 1;
  loading: string;

  ngOnInit(): void {
    this.loading = 'true';
    this._token.verifyToken().subscribe(
      (res) => {
        this.adminId = res.admin._id;
        localStorage.setItem('adminid', this.adminId);
        this._admin.getAdminById(this.adminId).subscribe((res) => {
          this.adminName = res.admin.name;
          this.adminEmail = res.admin.email;
          this._crud.readAllUsers().subscribe((res) => {
            this.users = res.users;
            setTimeout(() => {
              this.loading = '';
            }, 950);
          });
        });
      },
      (err) => {
        if (err instanceof HttpErrorResponse) {
          if (err.status === 400) {
            this._router.navigate(['/login']);
          }
        }
      }
    );
  }

  deleteUserById(id: string) {
    this._crud.deleteUser(id).subscribe(() => {
      this._crud.readAllUsers().subscribe((res) => {
        this.users = res.users;
        this.successMessage = 'User succesfully deleted';
      });
    });
  }

  removePopup() {
    this.successMessage = '';
  }
}

I believe the reason is because you have made HTTP calls without returning a specific type hence TS infers type Object我相信原因是因为您在没有返回特定类型的情况下进行了 HTTP 调用,因此 TS 推断类型Object

Below should work.下面应该工作。 Change code in the ngOnInit to将 ngOnInit 中的代码更改为

  ngOnInit(): void {
    this.loading = 'true';
    this._token.verifyToken().subscribe(
      (res: any) => {
        this.adminId = res.admin._id;
        localStorage.setItem('adminid', this.adminId);
        this._admin.getAdminById(this.adminId).subscribe((res: any) => {
          this.adminName = res.admin.name;
          this.adminEmail = res.admin.email;
          this._crud.readAllUsers().subscribe((res) => {
            this.users = res.users;
            setTimeout(() => {
              this.loading = '';
            }, 950);
          });
        });
      },
      (err) => {
        if (err instanceof HttpErrorResponse) {
          if (err.status === 400) {
            this._router.navigate(['/login']);
          }
        }
      }
    );
  }

note I have added (res: any)注意我已经添加(res: any)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM