从回调函数返回变量

时间:2018-11-15 19:09:12

标签: node.js gmail-api google-api-nodejs-client

我有这个代码

function LoadEmailIdsToCheck() {
  fs.readFile(config.googleCredentials, function processClientSecrets(
    err,
    content
  ) {
    if (err) {
      console.log("Error loading client secret file: " + err);
      //  return;
    }
    // Authorize a client with the loaded credentials, then call the
    // Gmail API.
    authorize(JSON.parse(content), getEmails);
  });
}


function authorize(credentials, callback) {
  var clientSecret = credentials.installed.client_secret;
  var clientId = credentials.installed.client_id;
  var redirectUrl = credentials.installed.redirect_uris[0];

  var OAuth2 = google.auth.OAuth2;

  var oauth2Client = new OAuth2(clientId, clientSecret, redirectUrl);
  ...
  }


function getEmails(auth) {
  const gmail = google.gmail({ version: "v1", auth });
  gmail.users.messages.list(
    {
      userId: "me"
    },
    (err, res) => {
      if (err) return console.log("The API returned an error!: " + err);
      else {
        response = res
       return response;
      }
    }
  );
}    

当我使用“ LoadEmailIdsToCheck()”触发代码时,我将不会返回回调函数。如果我执行console.log(res),它将打印出所需的数据,但无法执行“ return res”。

所以我的问题是为什么?我该如何解决这个问题?

0 个答案:

没有答案
相关问题