Uncaught Promise Rejection SyntaxError: Unexpected token u in JSON at position 0

Root Android

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 ?

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");

        });

    }

}

Ayush

It seems the message.json isn't a valid JSON. Instead, wrap the code in a try-catch so that error can be handled here. Also, try to log the msgs so that output can be viewed.

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);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Uncaught SyntaxError: Unexpected token u in JSON at position 0

React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Uncaught (in promise) SyntaxError: Unexpected token r in JSON at position 0

[Unhandled promise rejection: SyntaxError: Unexpected token i in JSON at position 7]

react : Unhandled Rejection (SyntaxError): Unexpected token < in JSON at position 0

Uncaught SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at Response.Body.json

VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>)

Flask and D3.js error - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

create-react-app Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 | MongoDB > Express > React.js

Redux app error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Selectize Uncaught SyntaxError: Unexpected token # in JSON at position 0

Uncaught SyntaxError: Unexpected token < in JSON at position 0 from Codeigniter response

undefined:1 undefined ^ SyntaxError: Unexpected token u in JSON at position 0

Saving to localstorage - SyntaxError: Unexpected token u in JSON at position 0

Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) at App.js:18:1 in console

Uncaught SyntaxError: Unexpected token , in JSON at position 10

Uncaught SyntaxError: Unexpected token a in JSON at position 2

VM126:1 Uncaught (in promise) SyntaxError: Unexpected token e in JSON at position 0 i think is about my prevent defaut element

Error when unit testing: "Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1"

SyntaxError: Unexpected token in JSON at position 0 Express

"SyntaxError: Unexpected token < in JSON at position 0"

NodeJS SyntaxError: Unexpected token in JSON at position 0

"parsererror" SyntaxError: Unexpected token < in JSON at position 0

SyntaxError: Unexpected token ‘ in JSON at position 0

SyntaxError: Unexpected token ' in JSON at position 0

SyntaxError: Unexpected token " in JSON at position 0

Uncaught SyntaxError: Unexpected token T in JSON at position 0 at JSON.parse (Asp.net MVC)

Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>)