简体   繁体   English

未捕获的 Promise Rejection SyntaxError: Unexpected token u in JSON at position 0

[英]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.

相关问题 未捕获的语法错误:JSON 中的意外标记 u 在位置 0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 未捕获(承诺)SyntaxError:JSON 中位置 0 的意外标记 &lt; - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse 如何修复“Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0”错误 - How to fix “Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0” ERROR Redux 应用程序错误:未捕获(承诺中) SyntaxError:意外令牌 &lt; 在 position 0 处的 JSON - Redux app error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 使用 vuejs - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 using vuejs Uncaught (in promise) SyntaxError: Unexpected token N in JSON at position 0 - Uncaught (in promise) SyntaxError: Unexpected token N in JSON at position 0 Django : Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - Django : Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 (React/Redux App) - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 (React/Redux App) React Js: Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM