使用 https 请求函数获取 api
Getting api using https request function
嗯,我有点问题
function getId(username){
const https = require("https")
let id = ""
let data = ``
https.get(`https://api.roblox.com/users/get-by-username?username=${username}`, (response) =>{
response.on('data', (chunk) => {
data += chunk;
})
response.on('end', () =>{
if(data){
id = JSON.parse(data).Id
}
})
})
.on('error', (error) => {
console.log(error)
})
return id
}
所以我的目标是使用 getId("IHZAQSTORM33")
和预期结果,它将 return 用户 ID (1684676332)。相反,它给了我(“”)。它给了我一个冒号
是的,我正在尝试连接到 roblox api。
使用 promise return 响应对象,如代码所示。
- nkron的评论也可以参考link:
function getId(username) {
const https = require('https');
let id = '';
let data = '';
const url = `https://api.roblox.com/users/get-by-username?username=${username}`;
return new Promise((resolve, reject) => {
https
.get(url, (response) => {
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
resolve(data);
});
})
.on('error', reject);
});
//return id
};
(async () => {
const responseObject = await getId('IHZAQSTORM33');
})();
- 有关 async 和 await 的更多信息,请参阅此 link:
嗯,我有点问题
function getId(username){
const https = require("https")
let id = ""
let data = ``
https.get(`https://api.roblox.com/users/get-by-username?username=${username}`, (response) =>{
response.on('data', (chunk) => {
data += chunk;
})
response.on('end', () =>{
if(data){
id = JSON.parse(data).Id
}
})
})
.on('error', (error) => {
console.log(error)
})
return id
}
所以我的目标是使用 getId("IHZAQSTORM33")
和预期结果,它将 return 用户 ID (1684676332)。相反,它给了我(“”)。它给了我一个冒号
是的,我正在尝试连接到 roblox api。
使用 promise return 响应对象,如代码所示。
- nkron的评论也可以参考link:
function getId(username) {
const https = require('https');
let id = '';
let data = '';
const url = `https://api.roblox.com/users/get-by-username?username=${username}`;
return new Promise((resolve, reject) => {
https
.get(url, (response) => {
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
resolve(data);
});
})
.on('error', reject);
});
//return id
};
(async () => {
const responseObject = await getId('IHZAQSTORM33');
})();
- 有关 async 和 await 的更多信息,请参阅此 link: