For some reason, every 25000 MS
(25 seconds), my ListLabels are being printed out twice. These duplicates are created exactly every 25 seconds. My code is below:
GOT TO LABELS ________________________________
Labels:
- [Imap]/Trash
- DRAFT
- UNREAD
- CATEGORY_UPDATES
- CATEGORY_PROMOTIONS
- INBOX
- CATEGORY_SOCIAL
- CATEGORY_PERSONAL
- CATEGORY_FORUMS
- [Imap]/Drafts
- Notes
- TRASH
- CHAT
- IMPORTANT
- [Imap]/Sent
- STARRED
- SPAM
- SENT
- [Imap]/Outbox
GOT TO LABELS ________________________________
Labels:
- [Imap]/Trash
- DRAFT
- UNREAD
- CATEGORY_UPDATES
- CATEGORY_PROMOTIONS
- INBOX
- CATEGORY_SOCIAL
- CATEGORY_PERSONAL
- CATEGORY_FORUMS
- [Imap]/Drafts
- Notes
- TRASH
- CHAT
- IMPORTANT
- [Imap]/Sent
- STARRED
- SPAM
- SENT
- [Imap]/Outbox
This is my main JS file, it's using modules such as express, as well as some of my own modules.
var express = require('express');
var app = express();
var fs = require('fs');
var getnewtoken = require('./auth.js');
var getauth = require('./getauth.js');
var getnewauth = require('./getnewauth.js');
var SCOPES = ['https://mail.google.com/'];
var google = require('googleapis');
var googleAuth = require('google-auth-library');
// getnewtoken(function (auth) { NEW ACCESS TOKEN
// listLabels(auth);
// });
listLabels();
function startproccess(token) {
getauth(token, function (auth) {
setInterval( function (){
listLabels(auth);
}, 25000)
});
}
function listLabels(auth) {
if(typeof auth === 'undefined'){ // First run, no auth key found
getnewtoken( function (token) {
startproccess(token);
});
console.log('auth not valid, going to startproccess');
}
var gmail = google.gmail('v1');
gmail.users.labels.list({
auth: auth,
userId: 'me@email.com',
}, function(err, response) {
if (err) { // API KEY NO LONGER VALID
console.log('The API returned an error: ' + err);
// Get new access token
getnewtoken( function (token) {
startproccess(token);
console.log("New token added");
return;
});
return;
}
var labels = response.labels;
if (labels.length == 0) {
console.log('No labels found.');
} else {
console.log('Labels:');
for (var i = 0; i < labels.length; i++) {
var label = labels[i];
console.log('- %s', label.name);
}
}
});
}
app.listen(3000, function(){
//console.log("Listening on port 3000");
});
答案 0 :(得分:1)
一次
auth==='undefined'
你需要打破这个功能,否则它会一直运行到最后。
在您的代码中,您调用函数startproccess两次,而它应该只调用一次。
如果auth ==='undefined',只需添加“return”,这样函数就会中断。