Please tell me how to return the data to the object data? from the request success in Mini Program.

my code is as follows:

App({
    data: {
        posts: {}
    },
    onLaunch(event) {
        // console.log("onLaunch");
        var that = this;
        swan.request({
            url: "http://www.sfatpaper.com/content.json",
            success: function (res) {
                console.log(res.data);
                that.setData({
                    posts: res.data.posts
                });
            },
            fail: function (err) {
                console.log(":" + err.errCode);
                console.log(":" + err.errMsg);
            }
        })      
    }
});

at present, the interface data can be read smoothly through ajax, among which there are two items of posts data

posts:Array(2)
    0:Object
    1:Object

I want to pass the data to the outer data object through that.setData so that other pages can call
but the error displays:

Uncaught (in promise) TypeError: that.setData is not a function

still hope to correct, what is the problem? Thank you!


App does not have setData , just use this.globalData
Page to point to the closure


success method, so this belongs to the closure, so this.setData () cannot be used directly in the success callback function. If we want to use it, we can assign this to another variable outside the closure. Const that = this original text link

Menu