0

i'm just newbie on nodeJs ,and i need your help

my utile.js file :

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
}                                                                                                                             

my intro.js file

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

        }
});

my data.json file :

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

But when i run the intro.js file by : node intro.js add --name="booktitle" --age="dsds" --birthday="1995" i got i nice erro [I spent 3 hours but i didn't find anything to solve that error ] Error :

/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: I didn't change anything in my yargs.js file .. Please what i'm doing wrong ? Thanks in advance !

Simon
  • 39
  • 7

1 Answers1

1

THe data.json needs to be array format for you to be able to push. Something like this

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

Also, since it is a normal json file you could simply require it as will instead of using fs to read it.

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

Hope this helps.

Ashish Modi
  • 7,529
  • 2
  • 20
  • 35