Monday, 12 June 2017

My notes-data.json file doens't have correct information

when i run the code below with the following in the command line :-

node name.js add --title = "123"

it should generate a file called notes-data.json which it does however when i open that JSON file to view the content it appears like this :-

[{"title":"=","body":""}]

instead of "title":"123"

console.log('Starting notes.js');

const fs = require('fs');

var addNote = (title, body) => {
  var notes = [];
  var note = {
    title,
    body
  };

  try {
    var notesString = fs.readFileSync('notes-data.json');
    notes = JSON.parse(notesString);
  } catch (e) {

  }

  var duplicateNotes = notes.filter((note) => note.title === title);

  if (duplicateNotes.length === 0) {
    notes.push(note);
    fs.writeFileSync('notes-data.json', JSON.stringify(notes));
  }
};

var getAll = () => {
  console.log('Getting all notes');
};

var getNote = (title) => {
  console.log('Getting note', title);
};

var removeNote = (title) => {
  console.log('Removing note', title);
};

module.exports = {
  addNote,
  getAll,
  getNote,
  removeNote
};

I would like some help on how to fix this?



via Demos

No comments:

Post a Comment