Javascript Gmail API使用nodejs、asnyc函数获取消息

Javascript Gmail API使用nodejs、asnyc函数获取消息,javascript,node.js,gmail-api,Javascript,Node.js,Gmail Api,我一直在努力使这项工作,但无法解决它 我需要检索所有邮件id,然后使用它们的id获取这些邮件消息。因为它不是异步的,所以在导出数据时我什么也得不到 function listMessages(auth) { const gmail = google.gmail({version: 'v1', auth}); let data; gmail.users.messages.list({ 'userId': 'mail address', 'q': 'some query' }, (e

我一直在努力使这项工作,但无法解决它

我需要检索所有邮件id,然后使用它们的id获取这些邮件消息。因为它不是异步的,所以在导出数据时我什么也得不到

function listMessages(auth) {
 const gmail = google.gmail({version: 'v1', auth});
 let data;
 gmail.users.messages.list({
  'userId': 'mail address',
  'q': 'some query'
 }, (err, res) => {
  res.data.messages.forEach(element => {
    console.log(element);
    mails.push(element.id);
  });
  mails.forEach((id) => {
    request = gmail.users.messages.get({
      'userId': 'sedatcyalcin@gmail.com',
      'id': id
  }, (err, res) => {
     data = res.data.snippet;
     console.log(data);
   });
 })

 });

}

我可以导出邮件,但不能导出数据

可能分为两个函数,如果一个函数返回,则promise可以解决它

const listMessages = auth => {
    return new Promise((resolve , reject)=>{
       const gmail = google.gmail({version: 'v1', auth});
       let data;
       gmail.users.messages.list({
         'userId': 'mail address',
         'q': 'some query'
       }, (err, res) => {
            res.data.messages.forEach(element => {
            console.log(element);
            mails.push(element.id);
            resolve(mails)
       });
    })
  })    
}

const getMessages = () => {
   listMessages().then(mails=>{
     mails.forEach((id) => {
        request = gmail.users.messages.get({
          'userId': 'sedatcyalcin@gmail.com',
          'id': id
      }, (err, res) => {
            data = res.data.snippet;
            console.log(data);
       });
     })
  })
}

伯德·博伊尔·德内,奥马兹·伊瑟·索鲁努·达哈·德塔伊尔·安拉森·亚德姆·埃特梅耶·阿勒姆。

我就是这样做的:

function getRecentEmail(auth) {
    const gmail = google.gmail({ version: 'v1', auth });
    // Only get the recent email - 'maxResults' parameter
    gmail.users.messages.list({ auth: auth, userId: 'me', maxResults: 1, }, function (err, response) {
        if (err) {
            console.log('The API returned an error: ' + err);
            return;
        }

        // Get the message id which we will need to retreive tha actual message next.
        var message_id = response['data']['messages'][0]['id'];

        // Retreive the actual message using the message id
        gmail.users.messages.get({ auth: auth, userId: 'me', 'id': message_id }, function (err, response) {
            if (err) {
                console.log('The API returned an error: ' + err);
                return;
            }
             email = response.data
             console.log(email)
        });
    }); }
我在这行下面的index.js文件中调用了它:

// Authorize a client with credentials, then call the Gmail API.
authorize(JSON.parse(content), getRecentEmail);

这是我基于TS的邮件列表解决方案

请注意,格式:“raw”返回base64。如果您只需要与查询匹配的每封电子邮件的片段,请删除该行

    const gmail = google.gmail({ version: 'v1', auth });
    let data: string | undefined;
    const mails: string[] = [];
    gmail.users.messages?.list(
        {
            userId: 'me',
            q: 'from: some query',
            maxResults: 1,
        },
        (err, res) => {
            if (!res || !res.data || !res.data.messages) {
                console.log('No Messages Found');
                return;
            }
            res.data.messages.forEach((message) => {
                console.log(`first api call: ${message}`);
                mails.push(message.id ?? '');
            });
            mails.forEach((id) => {
                const req = gmail.users.messages.get(
                    {
                        userId: 'me',
                        id,
                        format: 'raw',
                    },
                    (err, res) => {
                        // data = res?.data.snippet!;
                        console.log(res?.data);
                    }
                );
            });
        }
    );
};

您所说的导出是什么意思?在我看来,这个代码段是正确的。