1

this is (data) enter image description here

this is (data[0]) enter image description here

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)

2
  • How do you call test method? Please add more code Commented Jan 18, 2017 at 7:30
  • @yurzui here is my button code Commented Jan 18, 2017 at 7:32

1 Answer 1

2

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

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

12 Comments

yeah know its working if like that, but mine still doesnot work, maybe because its from http req and res please check my code again thanks
@Rommy your question doesn't include the relationship between Login() and the variables which you try to display. Can you add it?
@Rommy you've changed the question completely.. And I still can't see what is undefined as a variable..
well im sorry, i make it simple before so that every one can understand now its complicated, "data" is my array that i got from http request, but when im doing consolel.log(data.name) its show undefined , still not got it?
oh my god, the capital change everything, how stupid i am , thank's its working
|

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.