Discord.js/ROBLOX API Endpoint 问题返回“Typeerror: json.data is not iterable”
Posted
技术标签:
【中文标题】Discord.js/ROBLOX API Endpoint 问题返回“Typeerror: json.data is not iterable”【英文标题】:Discord.js/ROBLOX API Endpoint issue returning "Typeerror: json.data is not iterable" 【发布时间】:2021-09-20 06:31:20 【问题描述】:const Discord = require('discord.js');
const client = new Discord.Client();
const config = require('./config.json');
const token = config.token;
const globalprefix = config.prefix;
const fetch = require('node-fetch');
client.once('Ready', () =>
console.log('Client is ready!')
);
function EmbedMaker(GroupRank, Username)
const FunctionEmbedMessager = new Discord.MessageEmbed()
.setTitle(Username + "'s NUSA Role")
.setField('NUSA Role', GroupRank, false);
return FunctionEmbedMessager;
;
function GetUserIdFromUsername(Username)
(async () =>
const json = await fetch("https://api.roblox.com/users/get-by-username?username=" + Username)
.then((res) => res.json());
for (const info of json.data)
return info.Id
)();
;
client.on('message', msg =>
if (!msg.content.startsWith(globalprefix) || msg.author.bot) return;
const args = msg.content.slice(globalprefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
if (command === 'checknusarole')
if (!args[0]) msg.channel.send('**You did not specify a username to check the NUSA role of.**')
(async () =>
const json = await fetch("https://groups.roblox.com/v2/users/" + GetUserIdFromUsername(args[0]) + "/groups/roles")
.then((res) => res.json())
.catch(() => false);
if (!json.data) return
for (const info of json.data)
if (info.group === 758071)
console.log('its nusa')
const ReturnedEmbed = EmbedMaker(info.group.role.name, args[0])
msg.channel.send(ReturnedEmbed)
)();
);
client.login(token);
基本上,上面的代码应该向该 url 发送一个请求,从而导致 JSON 响应,每当我运行此代码时,它会说“类型错误:JSON.data 不可迭代”,我已经尝试过捕获,打破 for in 循环等。
【问题讨论】:
【参考方案1】:就像错误所说的那样,json.data is not iterable
。如果我是正确的,你应该把它放在你的.then()
而不是它之后。这是你应该如何做的。另外,如果res.json()
不是数组或映射(我认为它是一个对象),请改用for…in
。
const json = await fetch("https://groups.roblox.com/v2/users/" + GetUserIdFromUsername(args[0]) + "/groups/roles")
.then((res) =>
const json = res.json()
if (!json.data) return
for (const info of json.data)
if (info.group === 758071)
console.log('its nusa')
const ReturnedEmbed = EmbedMaker(info.group.role.name, args[0])
msg.channel.send(ReturnedEmbed)
)
【讨论】:
以上是关于Discord.js/ROBLOX API Endpoint 问题返回“Typeerror: json.data is not iterable”的主要内容,如果未能解决你的问题,请参考以下文章
ArcGIS JavaScript api Map update-end 停止触发
Twitter API v2:当参数有 start_time、end_time 时响应失败
使用 Big Query API 将数据提取到按时间分区的表中,但出现 SyntaxError: Unexpected end of input