1

Okay so I am running angular 2, and I am just trying to log a simple JSON object in the console but I get this enter image description here

UPDATE...

I have provided my error log below

enter image description here

I have looked on a fair few posts and people have similar issues e.g

Getting [object Object] while mapping http response in Angular 2

I have changed a few things around but still no dice!

Firstly here is my items2.json

{
  "Company": {
    "company_details": [
      {
        "test": "test"
      }
    ],
    "success": true
  }
}

My model ... 'company.ts'

export interface Company {
  company_details : CompanyDetails[];
  success : boolean;
}

export interface CompanyDetails {
  test: string;
}

Here is my component:

import {Component, OnInit} from '@angular/core';
import {Company} from "./models/test/company";
import { HttpClient } from '@angular/common/http';

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

  title = 'Test angular';

  constructor(private http : HttpClient) {}

  ngOnInit(): void {

    this.http.get<Company>('http://localhost:4200/assets/items2.json')
             .subscribe( data => console.log(data.company_details)
    );
  }   
}

Why I am receiving this error? how do I fix it ?

Thanks

seus
  • 568
  • 9
  • 31
  • Try check if this works. `this.http.get("http://localhost:4200/assets/api/items2.json").map(res => res.json()).subscribe( (data) => console.log(data.company_details), (err) => console.log(err) );` Or if it throws an erro – pritesh Nov 30 '17 at 05:55
  • Okay added it, thanks! – seus Nov 30 '17 at 05:59
  • Is it working or what happens when you go to `http://localhost:4200/assets/api/items2.json` directly – pritesh Nov 30 '17 at 06:02
  • @pritesh https://ibb.co/fUocpb , check that image. It outputs the JSON object – seus Nov 30 '17 at 06:05

1 Answers1

0

So I found the answer, it turns out the InMemoryDataService for the InMemoryWebApi, Interferes with Http, I have turned it off in my app.module.ts and it is now working.

Reference:

Angular2 / Error: Collection not found

seus
  • 568
  • 9
  • 31