Javascript “如何修复”;SyntaxError:JSON中位置1处的意外标记o;
因此,我试图发出一个命令,在聊天中发送随机GIF,但它不断给我错误。我该怎么办 我正在使用Discord.js对其进行编码,GIF由Giphy API支持Javascript “如何修复”;SyntaxError:JSON中位置1处的意外标记o;,javascript,discord.js,Javascript,Discord.js,因此,我试图发出一个命令,在聊天中发送随机GIF,但它不断给我错误。我该怎么办 我正在使用Discord.js对其进行编码,GIF由Giphy API支持 const Discord = require("discord.js"); const client = new Discord.Client(); client.on("message", async message => { if (message.content.startsWith(`${prefix}gif`)) {
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("message", async message => {
if (message.content.startsWith(`${prefix}gif`)) {
fetch(
"http://api.giphy.com/v1/gifs/random?api_key=PRIVATE"
).then(body => {
var body = JSON.parse(body)
message.channel.send({
embed: {
color: Math.floor(Math.random() * 16777214) + 1,
title: "**GIF Machine**",
description: "Here's your GIF!",
fields: [],
timestamp: new Date(),
image: {
files: [body.data.image_original_url]
},
footer: {
text: "Made with ❤️ created by Raymond#1725"
}
}
});
})
}
}
它给出了错误:
(节点:6732)未处理的PromisejectionWarning:SyntaxError:JSON中位置1处的意外标记o
在JSON.parse()处
试试这个:
client.on("message", async message => {
if (message.content.startsWith(`${prefix}gif`)) {
fetch(
"http://api.giphy.com/v1/gifs/random?api_key=PRIVATE"
)
.then(res=>res.json()) // changed
.then(body => { // changed
message.channel.send({
embed: {
color: Math.floor(Math.random() * 16777214) + 1,
title: "**GIF Machine**",
description: "Here's your GIF!",
fields: [],
timestamp: new Date(),
image: {
files: [body.data.image_original_url]
},
footer: {
text: "Made with ❤️ created by Raymond#1725"
}
}
});
})
}
}
试试这个:
client.on("message", async message => {
if (message.content.startsWith(`${prefix}gif`)) {
fetch(
"http://api.giphy.com/v1/gifs/random?api_key=PRIVATE"
)
.then(res=>res.json()) // changed
.then(body => { // changed
message.channel.send({
embed: {
color: Math.floor(Math.random() * 16777214) + 1,
title: "**GIF Machine**",
description: "Here's your GIF!",
fields: [],
timestamp: new Date(),
image: {
files: [body.data.image_original_url]
},
footer: {
text: "Made with ❤️ created by Raymond#1725"
}
}
});
})
}
}
不要尝试
JSON。解析
非JSON的内容?请看中的第一个示例,但如果我删除该示例,则将无法定义body
。不要尝试JSON。解析
非JSON的内容?请看中的第一个示例,但如果我删除该示例,则将无法定义body
定义…@RaymondHsu等一下,请求实际上是响应JSON吗?还是二进制gif?它响应gif,总之我修复了problem@RaymondHsu等等,请求实际上是响应JSON吗?还是二进制gif?它响应gif,不管怎样,我解决了这个问题