Eule
Eule

Reputation: 3

Angular 5 ReferenceError: require is not defined and TypeError: stream is undefined after implements a method to import JSON

I am new to Angular(5) and I've tried to import a JSON document from my assets to a component.
This JSON document contains information for some selections in my form, I will present in this component.
But actually, after I implements this method:

private getJson(url: string, http: HttpClient) {
    if (!url.search("[^]*.json")) {
      throw InvalidPathException;
    } else {
      return http.get(url);
    }
}

and subscribe to this,
I get in my browser the Errors:

ReferenceError: require is not defined

TypeError: stream is undefined

Does anyone know this problem and how to fix it?

Upvotes: 0

Views: 193

Answers (1)

Rak2018
Rak2018

Reputation: 965

Not recognized the error from the given code.. But I can share you another way to handle JSON document from your asset file in your angular5 code. You can change the below mentioned name and content of the JSON file according to your requirement. May be it will help you:

In the typescript file:

ngOnInit() {


this.http.get('assets/json/brandicon.json')
  .toPromise()
  .then(res => res.json().icons)
  .then(brandIcon => this.brandIcon = brandIcon);}

And in the src\assets\json\brandicon.json

{
"icons": [
    {
        "icon": "fa fa-500px"
    },
    {
        "icon": "fa fa-amazon"
    }]}

Upvotes: 0

Related Questions