[英]Uncaught Promise Rejection SyntaxError: Unexpected token u in JSON at position 0
I am trying to log a arg to json but its giving me 0 error means it not getting input or undefined but in console.log it displays the args clearly what can i do?我正在尝试将 arg 记录到 json 但它给我 0 错误意味着它没有得到输入或未定义但在 console.log 中它清楚地显示了 args 我该怎么办?
const fs = require('fs')
module.exports = {
name: "write",
category: "fun",
description: "Save 1 message in our database",
run: async (client, message, args) => {
let save = args.join(" ");
console.log(save)
let msgs = JSON.parse(fs.readFileSync("./message.json", "utf8"));
msgs[message.guild.id] = {
msgs: save
};
await fs.writeFile("./message.json", JSON.stringify(msgs), (err) => {
if (err) throw err;
message.channel.send("message logged");
});
}
}
It seems the message.json isn't a valid JSON.似乎 message.json 不是有效的 JSON。 Instead, wrap the code in a try-catch so that error can be handled here.
相反,将代码包装在 try-catch 中,以便可以在此处处理错误。 Also, try to log the msgs so that output can be viewed.
另外,尝试记录消息,以便可以查看output 。
try {
let msgs = JSON.parse(fs.readFileSync("./message.json", "utf8"));
msgs[message.guild.id] = {
msgs: save
};
await fs.writeFile("./message.json", JSON.stringify(msgs), (err) => {
if (err) throw err;
message.channel.send("message logged");
});
} catch(e) {
console.log(e.message);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.