0

I am new to javascript, and I am currently stuck in my javascript code.

What I am doing is that I am getting an object and I want to push that object into an array so that I can use that array later somewhere in my code. But every time I am getting empty array.

Please help, below is my code...

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);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
codex
  • 446
  • 1
  • 7
  • 17
  • you can't push to an object. use array if you want to push or extend the object – Varit J Patel Jul 12 '16 at 05:23
  • can you be more specific and add comments to your code like which is which? – jegtugado Jul 12 '16 at 05:26
  • @varit05 i am getting object in thread_id and this is in loop so is there any possibility to store all objects in to array? – codex Jul 12 '16 at 05:27
  • yes. you can store all objects in array using for loop – Varit J Patel Jul 12 '16 at 05:28
  • I used to face this issue when using json objects. So maybe you can explain what type of object it is, and then we can move forward. – Ajit Kumar Singh Jul 12 '16 at 05:32
  • @varit05 as you can see i am getting object in res variable and push it in emailmsgArray.push(res); but when i console it then got blank array console.log('email msg arr',emailmsgArray); – codex Jul 12 '16 at 05:32
  • It might be the case that, by the time values are pushed into array `console.log` statement is executed. So make sure all values are pushed into it and for testing purpose execute `console.log` in `setTimeout(fn(){}, 5000)` – Ashish Patil Jul 12 '16 at 05:40
  • What is the value of resp object? – jegtugado Jul 12 '16 at 05:41
  • object res is like Object {id: "XXXXXXXX", threadId: "YYYYYYYY", labelIds: Array[1], snippet: "some content here", historyId: "123456"…} – codex Jul 12 '16 at 05:51
  • `getMessage()` is an asynchronous function. So `emailmsgArray()` will be blank because you're logging it before the async functions have completed. – Barmar Jul 12 '16 at 05:54
  • But `thread_id_arr` should not be blank, I think you're mistaken about that. – Barmar Jul 12 '16 at 05:55
  • `thread_id_arr` will be blank in the last `console.log()` because `listMessages()` is also asynchronous. But it will not be blank in the `console.log` that's inside the `listMessages()` callback function. – Barmar Jul 12 '16 at 05:56
  • yes it is not blank inside listMessages() , as i am new will you please help me how to handle asynchronous function and how to get data in emailmsgArray from getMessage() @Barmar – codex Jul 12 '16 at 05:58
  • I linked to another question that explains the issue and how to deal with it. – Barmar Jul 12 '16 at 05:59

0 Answers0