javascript - Try Catch 中的 NodeJS JSON 写入错误

标签 javascript node.js json

正在学习 NodeJS 类(class),目前正在学习如何为笔记应用程序以 JSON 格式存储数据。如果 .json 文件不存在,则使用 try catch block 来处理错误。第一次执行 catch block 是因为文件为空,我返回一个空数组并将其存储在 JSON 文件中。第二次,当其中有数据时,catch block 也会被调用。在讲师视频中,相同的代码可以运行并附加数据。我们都使用 readFileSync 函数。

使用 yargs 进行参数解析。

我在这里缺少什么。

附加代码以及 app.js。

Notes.js


const getNotes = function () {
    return 'Your notes...'
}

const addNote = function(title, body){
    const notes = loadNotes()
    notes.push({
        title: title,
        body: body
    })
    saveNotes(notes)

}

const saveNotes = function(notes){
    const dataJSON = JSON.stringify(notes)
    fs.writeFileSync('notes.json', dataJSON,)
}

const loadNotes = function(){

    try{
        const dataBuffer = fs.readFileSync('notes.json')
        const dataJSON = dataBuffer.toString
        return JSON.parse(dataJSON)
    } catch(e){
        return []

    }

}

module.exports = {
    getNotes: getNotes,
    addNote: addNote
}

App.js

const yargs = require('yargs')
const notes = require('./notes.js')

const command = process.argv[2]

yargs.version('1.1.0')

yargs.command({

    command: 'add',
    describe: 'Add a new note',
    builder: { 
        title: {
            describe: 'Note title',
            demandOption: true,
            type: 'string'
        },
        body: {

            describe: 'Note body',
            demandOption: true,
            type: 'string'
        }
    },
    handler: function(argv){
       notes.addNote(argv.title, argv.body)
    }
})

yargs.command({

    command: 'list',
    describe: 'Lists all notes',
    handler: function(){

        console.log('Listing notes')
    }
})

yargs.command({

    command: 'read',
    describe: 'Reads all notes',
    handler: function(){

        console.log('Reading notes')
    }
})


yargs.command({

    command: 'remove',
    describe: 'Remove a note',
    handler: function(){
        console.log('Removing note')
    }
})

yargs.parse()

//console.log(yargs.argv)

最佳答案

您没有调用toString,只是访问该属性。将 .toString 更改为 .toString()

const loadNotes = function(){

    try{
        const dataBuffer = fs.readFileSync('notes.json')
        const dataJSON = dataBuffer.toString() // changed this
        return JSON.parse(dataJSON)
    } catch(e){
        return []

    }

}

此外,您还可以使用 utf8 作为 fs.readFileSync 的第二个参数,并避免 toString() 调用。

const dataJSON = fs.readFileSync('notes.json', 'utf8')
return JSON.parse(dataJSON)

关于javascript - Try Catch 中的 NodeJS JSON 写入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59338913/

相关文章:

JavaScript - "Filter"通过对象 - 最佳实践?

java - Android 尝试 GET http 请求时失败

java - JSON 对象未在 Struts2 Action 中设置变量

javascript - 将参数发送到本地 HTML 文件

javascript - 如何使用 react-spring 创建视差固定 header

javascript - 如何在 Sublime 3 中为 tern-js 设置跳转到方法定义键绑定(bind)

node.js - 我想在我的项目中使用 twilio api,但是当我在 typescript 文件中导入 twilio 时,它给出了一个错误

ios - 如何在 ionic 2/3 中上传图像服务器端

javascript - Express,如何将变量从路由传递到客户端的JS文件

javascript - VueJS 组件的数据列表不显示选项