The JS callback function is not valid for variable assignment

A strange phenomenon occurred when developing WeChat Mini Programs .
reference code is as follows:

//
var userName,portrait;
wx.getUserInfo({
    success: function (res) {
        var userInfo=res.userInfo;
        //wx.getUserInfo
        userName=userInfo.nickName;
        portrait=userInfo.avatarUrl;
    }
});
this.database.collection("Comment").add({
    data: {
        //:
        userName: userName,
        portrait: portrait
    }
});

has annotated the problem points in the code, and the
editor prompts Variable might not have been initialized,
why the variable assignment is not valid in the callback function? How to solve?

Whether
wx.getUserInfo is asynchronous userName or undefined


when success callback occurs, calling the save function


can be understood as Synchronize async. Wx.getUserInfo assigns values to global variables asynchronously. The value of the global variable when the this.database.collection is executed is the same as it was at the time of initialization (the global variable is declared not to be initialized, that is, undefined).

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-1bf4134-31ec3.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-1bf4134-31ec3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?