Home > OS >  How can I get multiple messages by IDs in one call in the Nylas Node SDK?
How can I get multiple messages by IDs in one call in the Nylas Node SDK?

Time:09-01

Im using the nylas sdk to retrieve some messages from my google Workspace inbox.

So far from the documentation I know I can get the messages with some search criteria like this:

const Nylas = require('nylas');

Nylas.config({
    clientId: <CLIENT_ID>,
    clientSecret: <CLIENT_SECRET>,
});
  
const nylas = Nylas.with(<ACCESS_TOKEN>);

nylas.messages.search("from:[email protected]").then(messages => {
    for (let message of messages) {
        console.log(message.subject);
    }  
});

If Im keeping track of a set of message ids is there a way I can get back those messages by passing the IDs?

CodePudding user response:

If you're on the Nylas Node SDK v6.2 or newer, you can use the findMultiple function that exists on the MessageRestfulModelCollection class. So lets assume we're collecting IDs like so:

const Nylas = require('nylas');

Nylas.config({
  clientId: <CLIENT_ID>,
  clientSecret: <CLIENT_SECRET>,
});
  
const nylas = Nylas.with(<ACCESS_TOKEN>);

const listOfIds = nylas
  .messages
  .search("from:[email protected]")
  .then(messages => messages.map(message => message.id));

You can fetch all the messages by the list of IDs in one call like so:

nylas.messages.findMultiple(listOfIds).then(messages => {
  for (let message of messages) {
    console.log(message.subject);
  }  
});

Happy coding!

  • Related