Sunday 30 April 2017

javascript array function can't be used

below is a piece of js code.When I run it with "node" command. It displays. "notes.filter" is not a function.If I comment "JSON.parse" line. It works. But video tutorial does have this line. So I am quite confused here. Could any one helps here. Thanks a lot.

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

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

  }

  console.log(Array.isArray(notes));
  var duplicateNote = notes.filter((note) => note.title === title);
  if (duplicateNote.length === 0) {
    notes.push(note);
    fs.writeFileSync('notes-data.json', JSON.stringify(note));
  }
};



via nathan

No comments:

Post a Comment