How to show the title of each Evernote note in Exp

2019-09-03 06:41发布

问题:

Do you know how to Show the title & content for all the Evernote notes in Express js ? https://github.com/evernote/evernote-sdk-js/blob/master/sample/express/routes/index.js https://github.com/evernote/evernote-sdk-js/blob/master/sample/express/views/index.jade

500 TypeError: C:\Users\lvcpp\Docs\evernote-sdk-js\sample\express\views\index.jade:31 29| li= session.notebooks[i].name 30| ul > 31| - for (var i = 0; i < session.notesMeta.length; ++i) 32| li= session.notesMeta.notes[i].title 33| hr 34| h2 Current status Cannot read property 'length' of undefined

29| li= session.notebooks[i].name
30| ul
> 31| - for (var i = 0; i < session.notesMeta.length; ++i)
32| li= session.notesMeta.notes[i].title
33| hr
34| h2 Current status


Express js web app:

//index.js:
...

exports.index = function(req, res) {
  if(req.session.oauthAccessToken) {
    var token = req.session.oauthAccessToken;
    var client = new Evernote.Client({
      token: token,
      sandbox: config.SANDBOX
    });
    var noteStore = client.getNoteStore();
    noteStore.listNotebooks(function(err, notebooks){
      req.session.notebooks = notebooks;
      res.render('index');
    });

    var notebook=noteStore.getDefaultNotebook(function (err, notebook){
      filter = new Evernote.NoteFilter();
      filter.notebookGuid = notebook.guid;
      resultSpec = new Evernote.NotesMetadataResultSpec();
      resultSpec.includeTitle=true;
      noteStore.findNotesMetadata(filter, 0, 100, resultSpec, function(err, notesMeta) {
        req.session.notes = notes;
        res.render('index');
        });
    });

  } else {
    res.render('index');
  }
};

// index.jade
...

ul
  - for (var i = 0; i < session.notebooks.length; ++i)
    li= session.notebooks[i].name
ul
  - for (var i = 0; i < session.notesMeta.length; ++i)
    li= session.notesMeta.notes[i].title

p.s. Thanks, @conch,
but unfortunately I get another error, after the fixes of code:

500 TypeError: C:\Users\lvcpp\Docs\evernote-sdk-js\sample\express\views\index.jade:31 29| li= session.notebooks[i].name 30| ul > 31| - for (var i = 0; i < session.notesMeta.notes.length; ++i) 32| li= session.notesMeta.notes[i].title 33| hr 34| h2 Current status Cannot read property 'notes' of undefined

29| li= session.notebooks[i].name
30| ul
> 31| - for (var i = 0; i < session.notesMeta.notes.length; ++i)
32| li= session.notesMeta.notes[i].title
33| hr
34| h2 Current status

We need to define the 'notes' property in index.js.

But how to, or do we really need it to deal with ?

In my console nodejs app all the things works without the special definition of notes:

function showlistNotes() {
  filter = new Evernote.NoteFilter();
  resultSpec = new Evernote.NotesMetadataResultSpec();
  resultSpec.includeTitle=true;

  noteStore.findNotesMetadata(filter, 0, 100, resultSpec, function(err, notesMeta) {
    if (err) {
      console.error('err',err);
    }
    else {
      console.log("\nFound "+notesMeta.notes.length+" notes in all the notebooks:");

      for (var i in notesMeta.notes) {
          console.log(i+": "+notesMeta.notes[i].title);
      }

      console.log('\nGet the contents of the selected note (see full list of notes, from all your notebooks):\nPlease insert the number from 0 to  ' + notesMeta.notes.length );
      process.openStdin().addListener("data", function(d) {
        var a=d.toString().trim();
        noteStore.getNote(notesMeta.notes[a].guid, true, true, true, true, function(err, note) {
          console.log(err || ENML.PlainTextOfENML(note.content, note.resources)  );
        }); 
      }); 
    }
  });
}

回答1:

It's failing because your js file sets a session variable called notes in the callback of noteStore.findNotesMetadata, but your jade file is expecting a session variable called notesMeta. Your jade file also has an indexing error (you should not be iterating through session.notesMeta, you should be iterating through session.notesMeta.notes).

The correct code:

index.js

noteStore.findNotesMetadata(filter, 0, 100, resultSpec, function(err, notesMeta) {
  req.session.notesMeta = notesMeta;
  res.render('index');
});

index.jade

ul
  - for (var i = 0; i < session.notesMeta.notes.length; ++i)
    li= session.notesMeta.notes[i].title