0

I am developing a form, where I will get value from a webapi and show to user.. Ok.. all part os get data from webapi are ok. Subscribe are returning the right Json, without problem

this is the angular program:

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.');
      });
  }
}

this is the instance for userall:

export interface UserAll {
  id: number;
  name: string;
  address1: string;
  address2: string;
  city: string;
  country: string;
  zipCode: string;
  phoneNumber: string;
 }

and this is the returns from console log.. console.log(result);

[{…}]0: address1: "xxxxxxxx" address2: "yyyyyyy" city: "fffffffffff" country: "Brazil" id: 1 name: "sjh fsdkjfh sakjhf " phoneNumber: "+5531969696969" user: null userId: "380de5fe-711a-4b4a-9130-4c86684c38df" zipCode: "6969696969" proto: Objectlength: 1__proto__: Array(0)

    console.log(result.name);

userdet.component.ts:67 undefined

    console.log(sessionStorage.getItem('currentUserId'));

userdet.component.ts:68 380de5fe-711a-4b4a-9130-4c86684c38df

    console.log(this.userall.name);

userdet.component.ts:69 undefined

i am confuse, because the json are returned by the subscribe, but the result.name is undefine.. WTH?

1 Answer 1

1

It seems this is an array with objects returned. Try result[0].name

Sign up to request clarification or add additional context in comments.

2 Comments

This worked for me... but i do not understand, the logic is the same in another proccess,a nd i do not used array to return values... [0].. but anyway, thanks for help...
yes you should check that. It might be defined in the array or a wrong function call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.