我是javascript的新手,我目前卡在我的javascript代码中。
我正在做的是我正在获取一个对象,并且我想将该对象推送到一个数组中,以便我可以在我的代码中的某个地方使用该数组。但每次我得到空阵。
请帮忙,下面是我的代码......
gapi.client.load('gmail', 'v1', function() {
var thread_id_arr = [];
listMessages(USER, '', function(resp) {
var threads = resp;
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var thread_id = thread.threadId; //here i am getting object
thread_id_arr.push(thread_id); // here i tried to push that object into array
getMessage(USER, thread_id, function(res) {
emailObject = res; // at this point also getting res as object
emailmsgArray.push(res); // want to push object into array
});
}
console.log('thread arr', thread_id_arr); //here getting blank array
console.log('email msg arr', emailmsgArray); //here getting blank
console.log('final msg', emailObject);
});
console.log('thread arr', thread_id_arr);
});
function listMessages(userId, query, callback) {
var getPageOfMessages = function(request, result) {
request.execute(function(resp) {
result = result.concat(resp.messages);
var nextPageToken = resp.nextPageToken;
if (nextPageToken) {
request = gapi.client.gmail.users.messages.list({
'userId': userId,
'pageToken': nextPageToken,
'q': query
});
getPageOfMessages(request, result);
} else {
callback(result);
}
});
};
var initialRequest = gapi.client.gmail.users.messages.list({
'userId': userId,
'q': query
});
getPageOfMessages(initialRequest, []);
}
function getMessage(userId, messageId, callback) {
var request = gapi.client.gmail.users.messages.get({
'userId': userId,
'id': messageId
});
request.execute(callback);
}