【问题标题】:Why do I keep getting an error saying I have unexpected end of JSON input while using nodeJS and yargs为什么我在使用 nodeJS 和 yargs 时不断收到错误消息,提示 JSON 输入意外结束
【发布时间】:2020-06-02 10:02:46
【问题描述】:

首先,我是 node.js 和 JavaScript 的菜鸟。

我正在尝试编写一个程序,使用 yargs 将用户的笔记存储在本地系统中。

我在处理程序中的路线如下-

  1. 首先创建一个包含笔记标题及其内容的对象。

  2. 我假设该文件始终在用户的本地系统中创建,并且要么是空的,要么有一些注释。

  3. 获取文件的内容,如果为空,则创建一个包含所有笔记数组的对象并将笔记推送给它。然后我在完成必要的 JSON 操作后将其写入文件。

  4. 如果文件中已经存在一个对象,我只需将新注释推送给它。

这是我的代码

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()

这是我收到的错误消息

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)

我已经尝试了很多方法,但无法摆脱那个错误。 有人可以帮助我。任何其他有助于我更好地理解这个概念的有用信息/资源将不胜感激。

任何关于在节点中使用 JSON(如 JSON.stringify、JSON.parse)的解释/资源都会非常有帮助。

提前致谢

【问题讨论】:

  • 如果文件不存在或为空(您正在尝试执行JSON.parse("")),您会收到该错误
  • notes.json 有问题,你能把它贴在这里
  • 首先。感谢您花时间帮助我。 notes.json 是一个空的 json 文件
  • 我搞定了。正如@GuyIncognito 正确指出的那样。我使文件包含一个空对象并且我工作了!
  • 感谢大家的帮助!

标签: arrays node.js json file-handling yargs


【解决方案1】:

我通过用空数组填充文件来解决它。

@GuyIncognito 指出,当文件为空时会弹出错误。

感谢大家的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 2021-04-06
    • 2018-10-03
    相关资源
    最近更新 更多