TypeError: notesExist.push is not a function?

TypeError: notesExist.push is not a function?

我只是 nodeJs 的新手,我需要你的帮助

我的utile.js文件:

const fs = require('fs');
    const addNotes = function(name,age,birthday){
        const notesExist = loadNotes()
        notesExist.push({
            name: name,
            age: age,
            birthday:birthday
        })

    }
    const loadNotes = function (){
        try{
            binaryVersion = fs.readFileSync('./JsonFile/data.json');
            stringVersion = binaryVersion.toString();
            dataParsed = JSON.parse(stringVersion);
            return dataParsed;
    }
    catch(err){
        return [];
    }
}
module.exports = {
    addNotes: addNotes,
    loadNotes: loadNotes
}                                                                                                                             

我的intro.js文件

yargs = require('yargs');
const utile = require('./utile.js');

yargs.command({
    command: 'add',
    describe: 'Adding new record',
    builder:{
        name:{
            describe:'note name',
            demandOption:true, // title option to be requires in the command line
            type:'string' //requie a string as title value
        },
        age:{
            describe:'note age',
            demandOption:true,
            type:'string' //require astring as body value
        },

        birthday:{
            describe:'note birthday',
            demandOption:true,
            type:'string' //require astring as body value
        },
    },
        handler: function(argv){
            //  console.log(chalk.green(chalk.red(argv.title)))
            /* console.log(chalk.green(chalk.green(argv.body)))  */
            utile.addNotes(argv.name,argv.age,argv.birthday);

        }
});

我的data.json文件:

{"name":"Hannani","age":25,"birthday":1995}

但是当我 运行 intro.js 文件时: node intro.js add --name="booktitle" --age="dsds" --birthday="1995" 我得到了很好的错误 [我花了 3 个小时,但我没有找到任何解决该错误的方法] 错误:

/home/simo/Documents/nodeJs/node_modules/yargs/yargs.js:1195
      else throw err
           ^

TypeError: notesExist.push is not a function
    at Object.addNotes (/home/simo/Documents/nodeJs/utile.js:34:20)
    at Object.handler (/home/simo/Documents/nodeJs/intro.js:56:19)
    at Object.runCommand (/home/simo/Documents/nodeJs/node_modules/yargs/lib/command.js:240:40)
    at Object.parseArgs [as _parseArgs] (/home/simo/Documents/nodeJs/node_modules/yargs/yargs.js:1107:41)
    at Object.parse (/home/simo/Documents/nodeJs/node_modules/yargs/yargs.js:566:25)
    at Object.<anonymous> (/home/simo/Documents/nodeJs/intro.js:86:7)
    at Module._compile (internal/modules/cjs/loader.js:1151:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)

ps:我没有更改 yargs.js 文件中的任何内容.. 请问我做错了什么?提前致谢!

data.json 需要数组格式才能推送。像这样

[{"name":"Hannani","age":25,"birthday":1995}]

此外,由于它是一个普通的 json 文件,您可以简单地要求它,而不是使用 fs 来读取它。

const data = require('./JsonFile/data.json')

希望对您有所帮助。