Angular 角度-订阅返回中未定义的值
我正在开发一个表单,我将从webapi中获得价值并向用户展示。。好啊所有部件操作系统都可以从webapi获取数据。Subscribe返回正确的Json,没有问题 这是角度程序:Angular 角度-订阅返回中未定义的值,angular,undefined,subscribe,Angular,Undefined,Subscribe,我正在开发一个表单,我将从webapi中获得价值并向用户展示。。好啊所有部件操作系统都可以从webapi获取数据。Subscribe返回正确的Json,没有问题 这是角度程序: import { Component, Input, ViewChild, OnInit } from '@angular/core'; import { ErrorMsgComponent } from '../../shared/error-msg/error-msg.component'; import { U
import { Component, Input, ViewChild, OnInit } from '@angular/core';
import { ErrorMsgComponent } from '../../shared/error-msg/error-msg.component';
import { UserallService } from 'src/app/services/userall.service';
import { Router } from '@angular/router';
import { UserAll } from '../../interfaces/userall';
import {NgForm} from '@angular/forms';
@Component({
selector: 'app-userdet',
templateUrl: './userdet.component.html',
styleUrls: ['./userdet.component.css']
})
export class UserdetComponent implements OnInit {
@Input() userall: UserAll = {} as UserAll;
@ViewChild(ErrorMsgComponent) errorMsgComponent: ErrorMsgComponent;
@ViewChild('f', {static: false}) myForm: NgForm;
public isDisabled = true;
public btnName = 'Alterar dados';
private userallOld = this.userall;
constructor(private userAllService: UserallService, private router: Router) {
}
ngOnInit(): void {
this.getUserData();
}
public editarForm(): void {
this.isDisabled = !this.isDisabled;
if (this.isDisabled) {
this.userall = this.userallOld;
this.btnName = 'Alterar dados';
}
else {
this.btnName = 'Cancelar';
}
}
onSubmit(): void {
this.modUserAll(this.userall);
}
modUserAll(userall: UserAll): void {
this.userAllService.modUserAll(userall)
.subscribe(
() => {
this.errorMsgComponent.setOk('Usuário alterado com Sucesso');
this.myForm.resetForm();
},
error => {
console.log(error);
this.errorMsgComponent.setError('Falha ao alterar usuario.');
});
}
getUserData(): void {
this.userAllService.getUser(sessionStorage.getItem('currentUserId'))
.subscribe(
(result: UserAll) => {
this.userall = result;
console.log(result);
console.log(result.name);
console.log(sessionStorage.getItem('currentUserId'));
console.log(this.userall.name);
this.myForm.name = this.userall.name;
},
error => {
console.log(error);
this.errorMsgComponent.setError('Falha ao ler dados do usuario.');
});
}
}
这是userall的实例:
export interface UserAll {
id: number;
name: string;
address1: string;
address2: string;
city: string;
country: string;
zipCode: string;
phoneNumber: string;
}
这是控制台日志的返回。。
控制台日志(结果)
[{…}]0:
地址1:“xxxxxxxx”
地址2:“yyyyyy”
城市:“fffffffffff”
国家:“巴西”
身份证号码:1
名称:“sjh fsdkjfh sakjhf”
电话号码:“+5531969”
用户:空
用户标识:“380de5fe-711a-4b4a-9130-4c86684c38df”
zipCode:“6969”
协议:对象长度:1__协议:数组(0)
userdet.component.ts:67未定义
console.log(sessionStorage.getItem('currentUserId'));
用户数据组件ts:68 380de5fe-711a-4b4a-9130-4c86684c38df
console.log(this.userall.name);
userdet.component.ts:69未定义
console.log(sessionStorage.getItem('currentUserId'));
我很困惑,因为json是由subscribe返回的,但是result.name是未定义的。。这似乎是一个返回了对象的数组。尝试结果[0]。名称这对我有效。。。但我不明白,在另一个过程中,逻辑是相同的,我不使用数组返回值。。。[0].. 但无论如何,谢谢你的帮助…是的,你应该检查一下。它可能是在数组中定义的,也可能是错误的函数调用。