2

I m learning native apps through javascript technologies, and I m trying to build a simple application using the pokemon api available only.

What I've done

I've created a simple component that list some pokemons, resulting from an http response :

import {Component, OnInit} from '@angular/core';
import 'rxjs/Rx';
import {Http} from '@angular/http';

@Component({
  selector:'pokemon-list',
  templateUrl: 'components/pokemonList/pokemonList.html'
})
export default class PokemonList implements OnInit{

  pokemons: Array<any>;

  constructor(private http:Http){
    this.pokemons = [];
  }

  ngOnInit() {
    this.getRemote(); // I pass on here
  }

  getRemote(){
    this.http
      .get('https://pokeapi.co/api/v2/pokemon/') // Throws the weird error
      .map(res => res.json())
      .subscribe(res => {
        this.pokemons = res.results;
      });
  }
}

My problem

When I start my application, I'm getting a weird error

Error in app.component.html:4:4 caused by: null is not an object (evaluating '_angular_platformBrowser.__platform_browser_private__.getDOM().getCookie')

I've to notice that this error only happens if the getRemote body is set with my http call. Moreover, when I set a default pokemon in my pokemon list, with the API result format like {name: 'Name', url: 'url}, the app is working and the pokemon well displayed.

If I remove the code like following, the app is working. It seems that I'm missing something with the Http module right there :

getRemote(){
    // App is running without the Http call
  }

NB : I'm using TS 2+ && I've set the Http module in my current module using :

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import {HttpModule} from '@angular/http';
import { NativeScriptModule } from "nativescript-angular/platform";
import PokemonList from './components/pokemonList/pokemonList.component';
import PokemonItem from './components/pokemonItem/pokemonItem.component';
import { AppComponent } from "./app.component";

@NgModule({
    declarations: [
      AppComponent,
      PokemonList,
      PokemonItem
    ],
    bootstrap: [AppComponent],
    imports: [
      NativeScriptModule,
      HttpModule
    ],
    schemas: [NO_ERRORS_SCHEMA]
})
export class AppModule { }

Any idea of what I'm doing wrong ?

Thanks for your help

1 Answer 1

2

In your NgModule instead of HttpModule, you should import the NativeScript wrapper NativeScriptHttpModule as shown here https://github.com/NativeScript/nativescript-sdk-examples-ng/blob/master/app/http/http-examples.module.ts#L32

import { NativeScriptHttpModule } from "nativescript-angular/http";
...
@NgModule({
    imports: [
        NativeScriptHttpModule,
        ...

NativeScriptHttpModule is a NativeScript wrapper of Angular’s HttpModule, a module that declares all of Angular’s HTTP-based services...

The usage of wrapper in NativeScript was needed as NativeScript does not "understand" of the DOM web specific properties used in Angular

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

Comments

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.