Sal-Sri
Sal-Sri

Reputation: 5

Getting an Error while trying to get email message body in last mail, Nodejs GMAIL api

Hope you all safe, My scenario is -> Get the message list and get the ID for each message. Get the message body based on the message ID. Finally, the text is Base64 converted,

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(); 

With this I am getting an error,

(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)

Appreciate your help on this matter

Upvotes: 0

Views: 204

Answers (1)

Algo7
Algo7

Reputation: 2166

Do

JSON.parse(res) and you will get your object. You can access the content and manipulate it afterward.

Upvotes: 1

Related Questions