![](/img/trans.png)
[英]How to fix "SyntaxError: Unexpected token o in JSON at position 1" in NodeJS/Express server
[英]How to fix “SyntaxError: Unexpected token o in JSON at position 1”
所以我試圖發出一個在聊天中發送隨機 GIF 的命令,但它一直給我錯誤。 我應該怎么辦?
我正在使用 Discord.js 對其進行編碼,並且 Giphy API 支持 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"
}
}
});
})
}
}
它給出了錯誤: (node:6732) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>)
嘗試這個:
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"
}
}
});
})
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.