Exploring the power of NativeScript and Angular 2's HTTP service

I am currently learning about native apps using JavaScript technologies and I am attempting to create a simple application utilizing the Pokemon API.

Progress So Far

I have developed a basic component that shows a list of pokemons retrieved 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(); // Here is where I run into issues
  }

  getRemote(){
    this.http
      .get('https://pokeapi.co/api/v2/pokemon/') // This is causing a strange error
      .map(res => res.json())
      .subscribe(res => {
        this.pokemons = res.results;
      });
  }
}

Issue Encountered

Upon launching my application, I encounter a peculiar error:

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

Note that this error only occurs when I make the http call within the getRemote function. Interestingly, if I manually set a default pokemon in the list with the format {name: 'Name', url: 'url}, the app functions correctly and displays the pokemon.

If I remove the code as shown below, the app works fine. It seems like there might be some issue with the Http module:

getRemote(){
    // App runs without the Http call
  }

NB: I am using TS 2+ and have included the Http module in my current module using the following:

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 insights on what could be going wrong here?

Appreciate your assistance

Answer №1

When setting up your NgModule, it's important to make sure you're using the appropriate modules. Instead of importing HttpModule, consider using the NativeScript wrapper called NativeScriptHttpModule. You can see an example of how to do this on this link: 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,
        ...

The NativeScriptHttpModule serves as a wrapper for Angular’s HttpModule, providing access to Angular’s HTTP-based services...

This wrapper is necessary in NativeScript because it doesn't fully support the DOM web properties used in Angular.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieving information from jQuery object using jQuery

I am utilizing ajax to retrieve information from the database. Once I have retrieved the information from the table, I am using json_encode to format the data into JSON. Then, I am utilizing parseJSON to display the data in JavaScript. After receiving the ...

"ng-new" is not currently present in the "@schematics/angular" collection

After removing npm and node, as well as homebrew, I restored them by downloading from the online site. I also installed the Angular cli using npm. Navigating to my desktop in the terminal, I entered: ng new mag-board to initiate my angular project. Howev ...

What is the best way to avoid multiple triggers of an event listener in a Vue Js Component?

When I use an event listener to call a specific function, it behaves strangely. Here is the code snippet: mounted() { window.addEventListener("message", this.call); }, methods: { call(e){ if (e.data === "test"){ ...

Issue with Sending Messages using SignalR in .NET and Angular

I am attempting to make a simple SignalR example work, following Microsoft's tutorial and using the Weatherforecast .NET/Angular SPA from Visual Studio as a starting point for a project I plan to integrate SignalR with in the future. You can find the ...

Utilizing VueJS v2 in tandem with Ezoic (or Adsense) for enhanced website monet

Recently, I updated my website to utilize VueJS v2, which was not used in the previous version. The main code is contained within the <div id="app"></div> element where Vue is initialized. However, I am facing a problem with the ads provided by ...

Can a "fragile export" be generated in TypeScript?

Testing modular code can be challenging when you have to export things just for the purpose of testing, which can clutter your code and diminish the effectiveness of features like "unused variable" flags on compilers or linters. Even if you remove a usage ...

Unable to utilize the concatMap() method in conjunction with the angular2 Http feature

Looking to chain multiple http requests, the number of requests can vary and they are not dependent on the previous request's result. The goal is to only keep the returned object from the last request. Received two suggestions on this forum thread. T ...

Utilizing Express JS to Optimize JPEG File Loading with Cache Headers

I have been working on implementing Cache-Control for my static image files. I have successfully set this header for HTML and JS files: https://i.stack.imgur.com/9VuWl.png However, I am facing challenges with JPEG files: https://i.stack.imgur.com/p52jm. ...

Concealing Website Elements with javascript based on class assignments

Despite my hesitation, I have to ask this question after unsuccessful searches in related articles. In my code, I have 7 nav links all with the same class. Due to the length of the HTML, I am looking for a way to hide contents until the link is clicked, an ...

The Angular HTTP request is coming back with a status of 0, even though I was anticipating

I need to access a server with various routes. The routes are as follows: app.use('/401', (req, res) => res.status(401).end()); app.use('/403', (req, res) => res.status(403).end()); app.use('/404', (req, res) => res. ...

The context of the selector causes an error to be thrown

Why does jQuery throw an exception when I attempt to select an element with context? The code is as follows: jQuery('b', "DAS<br/><br/><b>ID</b> = 02<br/><b>NAMA DAS</b> = CITARUM<br/><b>LUAS ...

basic handler in expressjs using the PUT method along with jQuery ajax

I am currently developing a web application utilizing a REST API for server communication. The backend is built with Node.js using Express.js. One issue I am running into is the inability to read the request body in PUT requests. Below is my client-side co ...

Creative Solution for Nested Forms

I am currently facing a challenge with nested forms in my PHP project. I need to include a form for AJAX image upload within another form so that I can pass the file path to the main form. The example code snippet is provided below; <form> <f ...

When does JSON overload become a problem?

Creating a bookmarking site similar to delicious has been my latest project. To ensure an optimized user experience, I have decided to fetch all the bookmarks from the database table and organize them into a JSON object containing essential data such as id ...

Rules for validating string and numeric combinations in Vuetify are essential for ensuring accurate

Looking for guidance on implementing Vuetify validation to enforce rules (using :rules tag on a v-text-field) in the format of AB-12345678 (starting with two letters followed by a hyphen and then an 8-digit number). I'm having difficulty achieving thi ...

Utilizing a single code base for both Web development with Angular 4 and Mobile app development with Ionic 3

I'm interested in finding out if I can utilize the same code base for both my Angular 4 web application and an Ionic 3 mobile application that I need to develop. As someone who is new to Ionic 3, I've been exploring the documentation and discove ...

Is it possible for jQuery UI Dialog to function similar to an iFrame?

Is there a way to set up my dialog box with a form so that when the form is submitted, only the dialog box changes instead of the browser location? ...

An Unexpected Typescript Error Occurred While Creating an RxCollection With RxDB

I'm new to RxDB and I've come across a strange Typescript error in my Electron project. Here are the relevant parts of my code: import RxDB, { RxCollection, RxDatabase } from "rxdb"; RxDB.plugin(require("pouchdb-adapter-idb") ...

iOS fixed positioning and scrolling

One of the current challenges we are facing involves implementing an action button similar to the one found in GMail for adding new content. This button needs to remain fixed during scroll. Here is the HTML code: <div class="addButton actionButton" on ...

Is it not possible to type a Specific Object Type as a Record?

I am looking to create a generic Table Row interface that will only allow objects with primitive attribute values. However, when I try to assign a specific type of object to the row, it fails. Why is this happening and how can I make it work? My goal is to ...