Returning a JSON object back from VM to page typescript


#1

Hey guys I’m trying to return a JSON object that gets called from an API in my view-model

my-view-model.ts

constructor() {
    super();
    requestor.get(url).then((res) => {
        responseObj = res;
        let users = responseObj.Users;
        if (!Users) {
            return false;
        } else {
            // console.log(JSON.stringify(users));
            return users;
        }
    });
}

my-page.ts


liveUserDataObjs = new UserViewModel;
console.log(JSON.stringify(liveUserDataObjs))

my-page.ts returns

{"_observers":{},“disableNotifications”:{}}

Any idea why my json object is not getting passed to my-page.ts?

Any help will be apprecited.


#2

At first look, it must return false every time :wink:

See first if clause


#3
  1. You don’t construct UserViewModel, there are no method call () brackets.
  2. You are making asynchonous calls in the constructor, it is not guaranteed that at the time of assignment you will have the users value.
  3. you check for Users while your variable is users
  4. You are attempting to return a value (users) in the constructor of an object… This appears wrong on a number of levels. Why not assign the value to a property?