Access the properties of a JSON called with http post in Angular 4

0

I happen to be making a post request to a URL with the following code


import { Component, OnInit } from '@angular/core';
import { UserService } from './../user.service';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; 
import { HttpClient } from '@angular/common/http';


@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {


  username: string = '';
  password: string = '';

  results: object;

  constructor(private userService: UserService, private router: Router, private http: HttpClient) { }

  ngOnInit() {

    if(this.userService.isValid()) {
        this.router.navigate(['home']);
        return;
    }
  }

  login() {

    var body = {
     email : this.username,
     password: this.password
    };

    this.http.post('http://127.0.0.1:8000/api/login', body).subscribe(data => {
      this.results = data;

      console.log(data);

    });

  }



}

which gives me back the following


{
  "success":
    {
      "data":
        {
          "id":6,"name":"nthny20",
          "email":"[email protected]",
          "user_role_id":"1",
          "created_at":"2018-01-20 19:51:40",
          "updated_at":"2018-01-20 19:51:40",
          "token":"Bearer eyJ0eXAiOiJKV1Qi"
      }
    }
  }

but when I try to put data.success.data.token which is the property I want to access, it sends me an error.

    
asked by Anthony Medina 21.01.2018 в 15:05
source

0 answers