如何从对象中获取键值?

时间:2019-04-25 07:54:40

标签: angular typescript rxjs

我已经在接口类型对象中从服务收集数据,但是无法从该对象中查找或过滤键。

错误为ERROR TypeError:无法读取未定义的属性'userAccess'     在ShowBranchuserComponent.push ../ src / app / pages / userrelated-page / show-branchuser / show-branchuser.component.ts.ShowBranchuserComponent.ngOnInit(show-branchuser.component.ts:42)

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { UserrelatedService } from '../userrelated-page.service';
import { UserRegister } from 'app/shared/model/Register';
import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';
import { RouteInfo } from 'app/shared/sidebar/sidebar.metadata';
import {map} from 'rxjs/operators';


@Component({
  selector: 'app-show-branchuser',
  templateUrl: './show-branchuser.component.html',
  styleUrls: ['./show-branchuser.component.scss']
})
export class ShowBranchuserComponent implements OnInit {
  showuser: UserRegister;
  pname;
  regularForm: FormGroup;
  access:RouteInfo[];

  constructor(private _AR:ActivatedRoute, 
    private _router: Router, 
    private _userRelated: UserrelatedService,
    private _fb: FormBuilder) { 
    this._AR.params.subscribe(data=>{this.pname = data['id']})
  }

  ngOnInit() {
    this._userRelated.getOneUser(this.pname).subscribe(data=> this.showuser = data);
    this.regularForm = this._fb.group({
      'id': [{value:'', disabled: true}, Validators.required],
      'name':[{value:'', disabled: true}, Validators.required],
      'username':[{value:'', disabled: true}, Validators.required],
      'role':[{value:'', disabled: true}, Validators.required],
      'status':[{value:'', disabled: true}, Validators.required],
      'state':[{value:'', disabled: true}, Validators.required],
      'pincode':[{value:'', disabled: true}, Validators.required],
      'mobileno':[{value:'', disabled: true}, Validators.required],
      'address':[{value:'', disabled: true}, Validators.required],
    });
   this.access = this.showuser.userAccess; // here showing error 
    console.log(this.showuser);
  }

  onReactiveFormSubmit(){

  }
}

2 个答案:

答案 0 :(得分:4)

返回可观察值是异步操作。因此,您编写的代码块将失败,因为此时showuser是不确定的。为了进一步了解这一点,您应该阅读JavaScript的队列和event loop

您应该将showuser.userAccess分配给access块内的.subscribe()属性:

ngOnInit() {
    this._userRelated.getOneUser(this.pname).subscribe(data=> {
      this.showuser = data;
      this.access = this.showuser.userAccess; 
      console.log(this.showuser);
     ]);
    this.regularForm = this._fb.group({
      'id': [{value:'', disabled: true}, Validators.required],
      'name':[{value:'', disabled: true}, Validators.required],
      'username':[{value:'', disabled: true}, Validators.required],
      'role':[{value:'', disabled: true}, Validators.required],
      'status':[{value:'', disabled: true}, Validators.required],
      'state':[{value:'', disabled: true}, Validators.required],
      'pincode':[{value:'', disabled: true}, Validators.required],
      'mobileno':[{value:'', disabled: true}, Validators.required],
      'address':[{value:'', disabled: true}, Validators.required],
    });

  }

答案 1 :(得分:1)

您在订阅功能的this.showuser中插入了数据,因此这是异步操作,但是您试图在同步操作中使用数据。异步操作需要花费一些时间才能使数据从服务器返回到客户端,因此您要做的是在数据到达之前将数据插入this.acess中。

要解决此问题,您可以将数据插入到订阅函数内的this.acess