async call for data from tree view only returns Promise, not data

Hi, having trouble loading data into my tree view and would be grateful for help.

My model is getData.js and includes these lines:
//getData.js
async function getAssets() {
securityToken = await getToken();
var url = "http://dashboard.myserver.com:8080/api/customer/abc123/devices?limit=100";
var response = await axios.get(url, { headers: { "X-Authorization": "Bearer " + securityToken }, });
return response.data.data
}
export { getAssets }

My view is test_tree.js and includes these lines in init():
//test_tree.js
this.loadData = function() {
var customerData = getAssets();
this.$$("hierarchyTree").parse(customerData);

Problem is, when I call getAssets(), it only returns a Promise, never the actual data. I've also tried getAssets().then(function(customerData) {this.$$("hierarchyTree").parse(customerData)}) but that produces a promise rejection, and a "TypeError: Cannot read property '$$' of undefined".

Comments

  • getAssets is async function.
    you need to await it to get result data
    var customerData = await getAssets();
    loadData is classic function.
    you need to convert it to arrow function to point this to view

    this.loadData = async () => {
        var customerData = await getAssets();
        this.$$("hierarchyTree").parse(customerData);
    }
    
  • Thank you very much, set me straight!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion