Manson Mamaril
Manson Mamaril

Reputation: 153

Nativescript: how to store HTTP values to public variables

I have some problem here, I'm using a mix of Angular2 and Nativescript's native JS to use HTTP because I was having problems with Angular2 HTTP before this implementation, but I'm having frustration on how to put the JSON response from the http request to the monthList variable so it can be read all over the place. Please help, Thank you.

Here's my code:

public monthList = [];


    constructor(location: Location, private page: Page, private router: Router, private testing: any) {
        this.router = router;
        this.testing = testing;
        let http = require("http");


        http.getJSON("link")
            .then(function (r) {
                // Argument (r) is JSON!
            }, function (e) {
                // Argument (e) is Error!
                console.log(e);
            });

    }

Upvotes: 1

Views: 177

Answers (2)

Misha
Misha

Reputation: 128

Inside your .then you set your return object to your monthList

this.monthList = r;

Try doing a console.dump(r) and see what is being returned. You may have to do something like r.xxx or however it is structured.

Then you can probably do something like

this.monthList.month 

and access the data of the month and so forth. Or you maybe have to do something like

this.monthList[0].month.

For example in my project I have a JSON object coming back that is nested and I need to set it as something like r.value.data.

Upvotes: 1

Mattew Whitt
Mattew Whitt

Reputation: 2424

Is there a specific issue you're running into? I would think its as simple as:

class SomeAngularClass {
  constructor() {
    http.getJSON("link")
      .then(function(r) {
        this.monthList.push(r);
      }, function (e) {
        console.log(e);
      });
  }
}

And then you could access it via:

SomeAngularClass().monthList;

Upvotes: 0

Related Questions