Giter VIP home page Giter VIP logo

Comments (5)

jerbob92 avatar jerbob92 commented on June 17, 2024 3

@manciuszz Your problem was that the toJson() is automatically called when you do console.log on the response object right? Because that's not something that is caused by nativescript-http. That's rather nativescript console.log automatically executing the methods. You could try to wrap it in a new Promise like this:

response.json = () => new Promise((resolve, reject) => {
  resolve(response.content.toJSON());
});

Or if you want to do it real proper:

response.json = () => new Promise((resolve, reject) => {
  try {
    const parsedJSON = response.content.toJSON();
    resolve(parsedJSON);
  } catch (e) {
    reject(e);
  }
});

That will probably fix it as that returns a promise that still needs to be resolved, while your previous implementation returns a Promise.resolve, and executes directly when you execute response.json(), instead of when you do response.json().then().

@eikaramba
I understand the problem. This library is a drop-in replacement for the NativeScript Core HTTP, and thus tries to replicate the behavior. This library behaves the same as core in this case, as can be seen here and here for Android and here and here for iOS.

I rather do not change that behavior by default.
However, I could introduce a "Safe mode" that would do extra checks before executing JSON.parse, what do you think?

from nativescript-http.

eikaramba avatar eikaramba commented on June 17, 2024

ok so this is not an issue in this library, i called content.toJson() on an empty response. however i still would argue that the toJson method should get some error handling for such cases. for example if the body is empty return either null or an empty object. but do not crash.

from nativescript-http.

manciuszz avatar manciuszz commented on June 17, 2024

... just gonna leave this here #50

from nativescript-http.

eikaramba avatar eikaramba commented on June 17, 2024

wow super interesting because i struggled with the console.log error myself and it was driving me nuts. so thank you for that explanation. i am not going crazy afterall :)

i wasn'T aware that the default nativescript library has the same "problem". so i might aggree with you leaving it like it is and putting the responsibility on the developer to catch this accordingly.

one thing could also be to output a console message and informing that the response was not a valid json so that it is clear why this is happening. again, i could see developers doing a simple console.log(response) to check and then wonder why it is not working at all.

from nativescript-http.

jerbob92 avatar jerbob92 commented on June 17, 2024

I have created a NativeScript runtime issue about this odd problem: NativeScript/android#1693
The main issue is that NativeScript called this method toJSON, which the console.log for some reason always calls, if it's not there, but a toString is there, it will execute toString.

from nativescript-http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.