简体   繁体   English

为什么我在使用 nodeJS 和 yargs 时不断收到错误消息,提示我有 JSON 输入意外结束

[英]Why do I keep getting an error saying I have unexpected end of JSON input while using nodeJS and yargs

Firstly, I am a noob to node.js and JavaScript.首先,我是 node.js 和 JavaScript 的菜鸟。

I am trying to write a program that stores the notes of a user in the local system using yargs.我正在尝试编写一个程序,使用 yargs 将用户的笔记存储在本地系统中。

My train of through in the handler is as follows-我在处理程序中的直通车如下 -

  1. First create the an object that contains the note's title and its contents.首先创建一个包含注释标题及其内容的 object。

  2. I assume that file is always created in the user's local system and is either going to be empty or have some notes.我假设该文件总是在用户的本地系统中创建,并且要么是空的,要么有一些注释。

  3. Get the contents of the file and if it is empty, create an object that has the array of all notes and push the note to it.获取文件的内容,如果它是空的,创建一个包含所有笔记数组的 object 并将笔记推送到它。 I then write that to file after doing the necessary JSON manipulations.然后我在完成必要的 JSON 操作后将其写入文件。

  4. If an object is already present in the file, I just push the new note to it.如果文件中已经存在 object,我只需将新注释推送给它。

Here is my code这是我的代码

const yargs = require("yargs")
const fs = require("fs")


yargs.command({

    command: "add",

    describe: "Add a note",

    builder: {
        title: {
            describe: "Title of the note to add",
            demandOption: true,
            type: "string"
        },
        note: {
            describe: "Contents of the note to add",
            demandOption: true,
            type: "string"
        }
    },

    handler: function() {

        let fullNote = {
            title: yargs.argv.title,
            note: yargs.argv.note
        }

        let fileContents = JSON.parse(fs.readFileSync("notes.json").toString())

        if(fileContents === undefined || fileContents === null){
            let newArrayJSON = {
                notes: []
            }

            newArrayJSON.notes[0] = JSON.stringify(fullNote)

            fs.writeFileSync("notes.json", newArrayJSON)

        } else {
            fileContents.notes.push(JSON.stringify(fullNote))

            fs.writeFileSync("notes.json", newArrayJSON)
        }

    }
})

yargs.parse()

And this is the error message I get这是我得到的错误信息

PS D:\Documents\Projects\Node\Node-NotesApp> node app.js add --title="To Buy" --note="Eggs"
D:\Documents\Projects\Node\Node-NotesApp\node_modules\yargs\yargs.js:1242
      else throw err
           ^

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Object.handler (D:\Documents\Projects\Node\Node-NotesApp\app.js:34:33)
    at Object.runCommand (D:\Documents\Projects\Node\Node-NotesApp\node_modules\yargs\lib\command.js:240:40)
    at Object.parseArgs [as _parseArgs] (D:\Documents\Projects\Node\Node-NotesApp\node_modules\yargs\yargs.js:1154:41)
    at Object.parse (D:\Documents\Projects\Node\Node-NotesApp\node_modules\yargs\yargs.js:599:25)
    at Object.<anonymous> (D:\Documents\Projects\Node\Node-NotesApp\app.js:54:7)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)

I have tried so many hings but cannot get rid of that error.我已经尝试了很多方法,但无法摆脱那个错误。 Could someone help me.有人可以帮助我。 Any other helpful information/resources to help me better understand this concept would be greatly appreciated.任何其他有助于我更好地理解这个概念的有用信息/资源将不胜感激。

Any explanations/resources on working with JSON in node (like JSON.stringify, JSON.parse ) would be be very helpful.有关在节点中使用 JSON 的任何解释/资源(如 JSON.stringify、JSON.parse )将非常有帮助。

Thanks in advance提前致谢

I solved it by populating the file with an empty array.我通过用空数组填充文件来解决它。

The error pops up when the file is empty as @GuyIncognito pointed out.正如@GuyIncognito 指出的那样,当文件为空时会弹出错误。

Thanks everyone for the help感谢大家的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在 Angular 10 中将数据推送到数组时会不断出错? - Why do I keep getting error while pushing data to Array in Angular 10? 为什么为什么不断出现“找不到符号”错误? - Why do I keep getting “cannot find symbol” error? 为什么我不断收到数组索引必须为正的错误? - Why do I keep getting an error that array indices must be positive? 为什么我不断收到找不到符号的错误 - Why do i keep getting a cant find symbol error 创建双精度数组时,为什么总是出现此错误? - Why do I keep getting this error when creating an array that is a double? 为什么我收到错误</script> ? 错误是 (Uncaught SyntaxError: Unexpected end of input) - Why am I receiving an error on </script> ? the error is (Uncaught SyntaxError: Unexpected end of input) 为什么我在使用 Arrays.sort() 时不断收到此错误? - Why do I keep getting this error when using Arrays.sort()? 我有一个意外的缓冲区溢出警告,为什么会这样? - I have an unexpected buffer overrun warning, why do I have that? 我不断收到编译器错误,说Rainfallmain.java:13:错误: <identifier> 预期。 - i keep getting a compiler error saying, Rainfallmain.java:13: error: <identifier> expected. 为什么我继续获取ArrayIndexOutofBoundsException? - Why do I keep on getting an ArrayIndexOutofBoundsException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM