Can't get response content in mithril

I am trying to make a request to the NodeJS API. For the client, I use the Mithril framework. I used my first example to query and retrieve data:

var Model = {
    getAll: function() {
        return m.request({method: "GET", url: "http://localhost:3000/store/all"});
    }
};

var Component = {
    controller: function() {
        var stores = Model.getAll();

        alert(stores); // The alert box shows exactly this: function (){return arguments.length&&(a=arguments[0]),a}
        alert(stores()); // Alert box: undefined
    },
    view: function(controller) {
        ...
    }
};

After doing this, I noticed that in Chrome Developer Tools, the API responds correctly as follows:

[{"name":"Mike"},{"name":"Zeza"}]

I can not find a way to get this data in the controller. They mentioned that using this method, var can hold undefineduntil the request is complete, so I followed the following example, adding:

var stores = m.prop([]);

Before the model and change the request to:

return m.request({method: "GET", url: "http://localhost:3000/store/all"}).then(stores);

I could do something wrong because I get the same result.

The goal is to get the data from the response and send it to the view for repetition.

+4
1

:

m.request - , m.request.then() , "store":

"function (){return arguments.length&&(a=arguments[0]),a}"

"stores()" undefined, async, , . "()" , . promises ( "then" ). , "then (param)", , .

:

, :

var Model = {
    getAll: function() {
        return m.request({method: "GET", url: "http://www.w3schools.com/angular/customers.php"});
    }
};

var Component = {

    controller: function() {
        var records = Model.getAll();

        return {
            records: records
        }
    },

    view: function(ctrl) {
        return m("div", [
            ctrl.records().records.map(function(record) {
                return m("div", record.Name);
            })
        ]);
    }
};

m.mount(document.body, Component);

, .

+2

Source: https://habr.com/ru/post/1616975/


All Articles