Skip to content

NS-angular 4.4.1 unable to retrieve local html snippets with NSHttp #1088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
corne-de-bruin opened this issue Nov 14, 2017 · 3 comments
Open

Comments

@corne-de-bruin
Copy link

Background:
After we upgraded our app to NativeScript 3.2 and nativescript-angular 4.2 we've ran into the following issue #949.

To solve this we've upgraded nativescript-angular to 4.4.1 and angular to 4.4.5.
But with this version we are no longer able to retrieve html files from the local file system. The content is properly loaded but returned in the error handler of the stream.

You can use the following example to recreate the issue:
https://github.com/corne-de-bruin/retrieve-local-html-example

Just clone the project and run:

npm i
tns prepare android
tns run android

In the console you will see that it end's up in the error handler of the request.

If I'm right it's related to the following function in "nativescript-angular/http-client/http-utils.ts"

return new Observable((observer: Observer<T>) => {
        if (nsFileSystem.fileExists(url)) {
            const localFile = nsFileSystem.fileFromPath(url);
            localFile.readText()
                .then((data) => {
                    try {
                        const json = JSON.parse(data);
                        observer.next(successResponse(url, json, 200));
                        observer.complete();
                    } catch (error) {
                        // Even though the response status was 2xx, this is still an error.
                        // The parse error contains the text of the body that failed to parse.
                        const errorResult = { error, text: data };
                        observer.error(errorResponse(url, errorResult, 200));
                    }
                }, (err: Object) => {
                    observer.error(errorResponse(url, err, 400));
                });
        } else {
            observer.error(errorResponse(url, "Not Found", 404));
        }
    });

This tries to parse all local loaded files to JSON.

@tsonevn
Copy link
Contributor

tsonevn commented Nov 14, 2017

Hi @corne-de-bruin,
I have just tested the sample project while using the below-attached dependencies in the project package.json file, however, was unable to recreate the issue on my side.

{
  "description": "NativeScript Application",
  "license": "SEE LICENSE IN <your-license-filename>",
  "readme": "NativeScript Application",
  "repository": "<fill-your-repository-here>",
  "nativescript": {
    "id": "org.nativescript.testapp",
    "tns-android": {
      "version": "3.3.0"
    }
  },
  "dependencies": {
    "@angular/animations": "~4.4.1",
    "@angular/common": "~4.4.1",
    "@angular/compiler": "~4.4.1",
    "@angular/core": "~4.4.1",
    "@angular/forms": "~4.4.1",
    "@angular/http": "~4.4.1",
    "@angular/platform-browser": "~4.4.1",
    "@angular/router": "~4.4.1",
    "nativescript-angular": "~4.4.0",
    "nativescript-theme-core": "~1.0.2",
    "reflect-metadata": "~0.1.8",
    "rxjs": "~5.4.2",
    "tns-core-modules": "~3.3.0",
    "zone.js": "~0.8.2"
  },
  "devDependencies": {
    "babel-traverse": "6.26.0",
    "babel-types": "6.26.0",
    "babylon": "6.18.0",
    "lazy": "1.0.11",
    "nativescript-dev-typescript": "0.5.0",
    "typescript": "2.4.2"
  }
}

The steps I just follow are:

  1. replace the package.json with the attached one.
  2. remove node_modules and platforms folders
    3, run tns run android

Let me know if I am missing something. I build the project, while using NAtiveScript CLI version 3.3.0.

@corne-de-bruin
Copy link
Author

corne-de-bruin commented Nov 14, 2017

Hi @tsonevn

I've tried it again with the package json you provided and the nativescript-cli 3.3.0 and it still doesn't work.

I've updated the sample project to make it a bit more clear

If you see the same result as the below screenshot means that it doesn't work.
In stead of Error! you should see:
This is test content!

schermafbeelding 2017-11-14 om 16 22 06

@NickIliev NickIliev added the http label Jan 8, 2019
@NickIliev
Copy link

The responseType for GET of HttpClient can be of the follwing:

 responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';

It seems that local files are automatically defaulting to JSON as an option to parse which could be improved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants