好的,我想在函数中定义请求返回的状态代码。 但它不起作用,它说变量尚未定义,我不知道如何解决
这是我的代码
https = require('https')
var available;
function checkAvailability(token, password) {
// Payload
const data_send = JSON.stringify({
password: password
})
// Settings
const settings = {
hostname: 'discord.com',
path: `/api/v6/users/@me`,
method: 'PATCH',
headers: { 'Authorization': token, 'Content-Type': 'application/json' },
}
// Send the request
const send = https.request(options, async res => {
if(res.statusCode == 400 || res.statusCode == 401) {
available = 'Yes'
} else {
available = 'No'
}
}).on('error', e => {
console.error(e);
});
send.write(data_send);
send.end();
console.log(available)
}
错误在于,我不知道如何在请求之外导出变量,因为我需要它们。
(node:7184) UnhandledPromiseRejectionWarning: ReferenceError: available is not defined
at checkAvailability (C:\Users\root\Desktop\SG\structures\functions.js:131:17)