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?