Monday, 5 June 2017

Multiple dependent API calls in Node.js and express

Overview: I want to make a request to google gmail api. I get an array of draftIds. I then iterate over this array, making a call for each draftId, which gives me a specific draft. I then iterate over this data, to get the subject line. I want to store the subject line in an array. Once all are done, send back to the client the list of draft subjects.

I have the following code, here I am console.logging the desired data. How can I store these, then call res.json(finalizedData)

var google = require('googleapis'); var client = require('./authclient')

client.execute(['https://www.googleapis.com/auth/gmail.modify'], function(tokens){
  var gmail = google.gmail('v1');
    gmail.users.drafts.list({
      access_token: tokens.access_token,
      userId: 'me'}, function(err, drafts){
        drafts.drafts.forEach((draft)=>{
        gmail.users.drafts.get({
          userId: 'me',
          id: draft.id,
          access_token: tokens.access_token }, function(err, draftData{
            draftData.message.payload.headers.forEach(function(d){
                  if(d.name === 'Subject' && d.value.length){
                    console.log(d.value)
                  }
                })

            })
        })

    })    

})



via ZenStein

No comments:

Post a Comment