【发布时间】: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));这个怎么样?