TypeError:notesExist.push不是函数吗?

问题描述 投票:0回答:1

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

my 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文件中的任何内容。请问我做错了什么?预先感谢!

node.js yargs
1个回答
0
投票

TH data.json必须为数组格式,然后才能推送。像这样的东西

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

此外,由于它是普通的json文件,因此您可以直接按需使用它,而不用使用fs来读取它。

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

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.