简体   繁体   中英

I am unable to retrieve the message by its id in gmail-api

So I have created a class Check with two methods checkInbox() and getMail().

The checkInbox() retrieves all the mails and the getMail(msgId) will retrive the mail with the input Id.

The checkInbox() works fine but the getMail() method gives the same output as checkInbox() ie lists all the mails instead of returning the requested mail.

class Check{

    constructor(auth){
        this.me = 'mygmailid';
        this.gmail = google.gmail({version: 'v1', auth});
        this.auth = auth;
    }

    checkInbox(){
        this.gmail.users.messages.list({
            userId: this.me
        }, (err, res) => {
            if(!err){
                console.log(res.data);
            }
        })
    }

    getMail(msgId){
        this.gmail.users.messages.get({
            'userId': this.me,
            'id': msgId
        }, (err, res) => {
            if(!err){
                console.log(res);
            }
        });
    }

}

var obj = new dem(auth);
obj.getMail('someRandomIdNumber');

I haven't attached the authorization code as it works fine. Also there is no error in importing and exporting the class.

This is exactly what you should do to fetch a gmail-message:

  const response = await this.gmail.users.messages.get({
    auth: this.oAuth2Client,
    userId: this.gmailAddress,
    id: messageId,
    format: 'full'
  })

you can find the whole implementation here . It's a light wrapper on Gmail-APIs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM