【问题标题】:Getting an Error while trying to get email message body in last mail, Nodejs GMAIL api尝试在最后一封邮件中获取电子邮件正文时出错,Nodejs GMAIL api
【发布时间】:2020-08-04 08:19:06
【问题描述】:

希望大家平安, 我的场景是 -> 获取消息列表并获取每条消息的 ID。根据消息 ID 获取消息体。最后将文字进行Base64转换,

const gmailGetMessagesAsync = promisify(gmail.users.messages.get);
const gmailListMessagesAsync = promisify(gmail.users.messages.list);

let res = await gmailListMessagesAsync({
        auth: oauth2Client,
        userId: 'me'
    });
    const newestMessageId = res.messages[0].id;

res = await gmailGetMessagesAsync({
        auth: oauth2Client,
        userId: 'me',
        id: newestMessageId
    });

 const base64mailBody = res.payload.parts[0].body.data;
const mailBody = new Buffer(base64mailBody, 'base64').toString(); 

这样我得到一个错误,

(node:9041) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
    at main (/NODEGMAIL/getLabels.js:42:37)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:9041) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

感谢您对此事的帮助

【问题讨论】:

  • 你能做一个.catch() 吗? rest 对象不包含 message 属性
  • @ Aviv Lo - 请提供更多解释,我是这个 NODE.js 的新手,这就是原因。
  • Ok.`做一个console.log(res)`看看res长什么样子?
  • console.log(res); --> 输出[object Object]
  • console.log(JSON.parse(res)); 这个怎么样?

标签: node.js gmail-api


【解决方案1】:

JSON.parse(res) 你会得到你的对象。您可以访问内容并在之后对其进行操作。

【讨论】:

    猜你喜欢
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2016-10-27
    • 2016-11-30
    • 2020-01-11
    • 2018-08-25
    相关资源
    最近更新 更多