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

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea1031-5c93c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea1031-5c93c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?