1
저는 여전히 JS에서 멍청한 사람입니다. 그래서 저는 다음과 같은 질문을 가지고 있습니다.다른 함수에서 전역 적으로 변수를 정의하는 함수에서 데이터 세트에 액세스하기
var twoFactorAuthCode;
fs.readFile('file.2fa', function (err, data) {
if (err) {
logger.warn('Error reading neyotbot1.2fa. If this is the first run, this is expected behavior: '+err);
} else {
logger.debug("Found two factor authentication file. Attempting to parse data.");
twoFactorAuth = JSON.parse(data);
SteamTotp.getTimeOffset(function (error, offset, latency) {
if (error) {
logger.warn('Error retrieving the time offset from Steam servers: ' + error);
} else {
timeOffset = offset + latency;
}
});
console.log(twoFactorAuthCode); //returns undefined
twoFactorAuthCode = SteamTotp.getAuthCode(twoFactorAuth.shared_secret, timeOffset);
console.log(twoFactorAuthCode); //returns what is expected
}
console.log(twoFactorAuthCode); //also returns what is expected
});
client.logOn({
accountName: config.username,
password: config.password,
twoFactorCode: twoFactorAuthCode //this is still set as undefined
});
내 문제가 변수 twoFactorAuthCode이는 fs.readFile() 함수에 값을 할당되는 글로벌 범위를 가지고 있지만, 그것은 다음 함수를 통해 데이터를 전송하지 않는다는 것입니다 :이 JS이 client.logOn().
제 질문은 변수를 사용하여 첫 번째 함수에서 두 번째 함수로 데이터를 가져올 수 있습니까? 나는 이것으로 나를 도울만큼 단순한 것을 찾을 수 없었다.