简体   繁体   中英

angular 2 array list undefined

this is (data) 在此处输入图片说明

this is (data[0]) 在此处输入图片说明

here is my html

<div  class="page">
<paper-card heading="Login Form">
  <div class="card-content">
    <paper-input [(ngModel)]="user.username" label="Username"></paper-input>
    <paper-input [(ngModel)]="user.password" label="Password" type="password"></paper-input>
  </div>
  <div class="card-actions" align="end">
    <paper-button class="login" id="Login" raised disabled (click)="Login(user)">Login</paper-button>
  </div>
</paper-card>
</div>

<paper-dialog id="wrong" modal>
  <p>Your username or password wrong please check again.</p>
  <div class="buttons">
    <paper-button dialog-confirm autofocus>Ok..</paper-button>
  </div>
</paper-dialog>

here is my full code, when im trying to get array from http res its show undefined

Login(user){
        this._httpprovider.httpReq('http://localhost:5000/user','POST',{username:user.username, password:user.password},null).subscribe((data)=>{ 
                if (data.length > 0 ){
                    this._userdetails.setDetails(data);
                    this._router.navigate(['MainMenu']);
                    console.log(data.name); <<<<<<< here is the part
                }else {
                    var wmodal :any = document.getElementById('wrong');
                    wmodal.open();
                }

            });
        }

but if im doing the console.log(data); it shows the result

Array[1]
0
:
Object
Name
:
"Admin"
Password
:
"Admin"
SBU
:
"IT"
Updateon
:
"2016-12-21T17:50:21.393Z"
UserName
:
"Admin"
__proto__
:
Object
length
:
1
__proto__
:
Array[0]

here is the array i get from http req

[{"UserName":"Admin","Password":"Admin","Name":"Admin","SBU":"IT","Updateon":"2016-12-21T17:50:21.393Z"}]

what i want to get is the name = admin

so thats why im doing console.log(data.name)

Updated answer after OP edited the post

If data is an array first you need to select the correct element.

Like

Login(user){
        this._httpprovider.httpReq('http://localhost:5000/user','POST',{username:user.username, password:user.password},null).subscribe((data)=>{ 
                if (data.length > 0 ){
                    this._userdetails.setDetails(data);
                    this._router.navigate(['MainMenu']);
                    console.log(data[0].Name); //<<<<<<< Assuming your data is correct
                }else {
                    var wmodal :any = document.getElementById('wrong');
                    wmodal.open();
                }
        });
}

old answer

You are trying to access a local variable with this in this code.

test(){
  var b = []
  b = this.a; //<-- add this
  console.log(b.Name); //<-- remove this
}

Also a is an object in your field then you assign it to an empty array inside test.

Working example: http://plnkr.co/edit/ekmcCaiszsPoM1gV6NRQ?p=preview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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