Luke Pring Luke Pring - 4 months ago 26
jQuery Question

Defining multidimensional arrays

I'm trying to work with the Gmail API to create a mailing function on a CMS, though I am having a problem with creating the array.

$Message = {};
$Message['Date'] = getHeader(message.payload.headers, 'Date');
$Message['From'] = getHeader(message.payload.headers, 'From');
$Message['Subject'] = getHeader(message.payload.headers, 'Subject');
$Message['Reply-to'] = getHeader(message.payload.headers, 'Reply-to');
$Message['Body'] = getBody(message.payload);
$Message['ID'] = message.id;

$PartCount = 0;

for (var _ in message['payload']['parts']) $PartCount++;

if($PartCount > 2) {
$Message['hasAttachment'] = 'true';
} else {
$Message['hasAttachment'] = 'false';
}

if($PartCount > 2) {
$AttachmentCount = 1;
while($AttachmentCount <= $PartCount) {
$LocalAttachmentCount = ($AttachmentCount - 1);
if($Message.Subject == "AttachmentTest") {
console.log($Message);
console.log(message);
}
$Message['Attachment'] = {};
$Message['Attachment'][($LocalAttachmentCount)] = {};
$Message['Attachment'][($LocalAttachmentCount)]['FileName'] = message['payload']['parts'][$AttachmentCount]['filename'];
$AttachmentCount++;
}
}


The error I am receiving is as follows

getgmail:551 Uncaught TypeError: Cannot read property 'filename' of undefined
appendMessageRow
@ getgmail:551(anonymous function)
@ cb=gapi.loaded_0:628(anonymous function)
@ cb=gapi.loaded_0:601(anonymous function)
@ cb=gapi.loaded_0:167_.T.E.__cb
@ cb=gapi.loaded_0:99g
@ cb=gapi.loaded_0:93c
@ cb=gapi.loaded_0:84


Any suggestion would be appreciated as I'm going round in circles at the moment.

Much appreciated, Luke.

Edit: I have attached an image of the data that i am working with. It is written in Jquery, but my main language is PHP :)

enter image description here

Original JSON

{"id":"155e987feb707c32","threadId":"155e987feb707c32","labelIds":["SENT","INBOX","IMPORTANT"],"snippet":"Test for getting attachments in Expert.","historyId":"873165","internalDate":"1468501851000","payload":{"mimeType":"multipart/mixed","filename":"","headers":[{"name":"MIME-Version","value":"1.0"},{"name":"Received","value":"by 10.237.32.135 with HTTP; Thu, 14 Jul 2016 06:10:51 -0700 (PDT)"},{"name":"Date","value":"Thu, 14 Jul 2016 14:10:51 +0100"},{"name":"Delivered-To","value":"email@address.co.uk"},{"name":"Message-ID","value":"<CAJvKzxJXb4ERig_Lj9PE7mFNDZLMK6cuKpa0uKba5f1KRvE-=A@mail.gmail.com>"},{"name":"Subject","value":"AttachmentTest"},{"name":"From","value":"Sender <email@address.co.uk>"},{"name":"To","value":"Receiver <email@address.co.uk>"},{"name":"Content-Type","value":"multipart/mixed; boundary=001a113e81f03989b70537983b20"}],"body":{"size":0},"parts":[{"mimeType":"multipart/alternative","filename":"","headers":[{"name":"Content-Type","value":"multipart/alternative; boundary=001a113e81f03989aa0537983b1e"}],"body":{"size":0},"parts":[{"partId":"0.0","mimeType":"text/plain","filename":"","headers":[{"name":"Content-Type","value":"text/plain; charset=UTF-8"}],"body":{"size":41,"data":"VGVzdCBmb3IgZ2V0dGluZyBhdHRhY2htZW50cyBpbiBFeHBlcnQuDQo="}},{"partId":"0.1","mimeType":"text/html","filename":"","headers":[{"name":"Content-Type","value":"text/html; charset=UTF-8"}],"body":{"size":62,"data":"PGRpdiBkaXI9Imx0ciI-VGVzdCBmb3IgZ2V0dGluZyBhdHRhY2htZW50cyBpbiBFeHBlcnQuPC9kaXY-DQo="}}]},{"partId":"1","mimeType":"text/plain","filename":"text.txt","headers":[{"name":"Content-Type","value":"text/plain; charset=US-ASCII; name=\"text.txt\""},{"name":"Content-Disposition","value":"attachment; filename=\"text.txt\""},{"name":"Content-Transfer-Encoding","value":"base64"},{"name":"X-Attachment-Id","value":"f_iqmc263q1"}],"body":{"attachmentId":"ANGjdJ-R1qc49sxpIHWRJpj6_zPF70sshHhNsKvCAqxHkYzltyxfwme_or6vK069GaKpDJzjHrtVsZqtQkHjTUSzSYg5yUOqM5y618mmOOIJUL_NnnOvrmTWVh0fKfMY1FAMGy1RJeUkb7vrbdv6lYXnAHYwojPMVx1aIG6DPWqqieQdBdE9p0704yxvbfoUu9XjeueV1FRs4b1B7x9mcS8Jj_7-uKo7a4l9nVr2HfrdnL4RS8D1JwHe1-DZiF4xiEX2j4Gre-36FooEcOnuZ_1RQryecIh5Fq_kobtFco-4_jd-w8gIMZYSHVDzUiw","size":12}},{"partId":"2","mimeType":"image/png","filename":"White.png","headers":[{"name":"Content-Type","value":"image/png; name=\"White.png\""},{"name":"Content-Disposition","value":"attachment; filename=\"White.png\""},{"name":"Content-Transfer-Encoding","value":"base64"},{"name":"X-Attachment-Id","value":"f_iqmc1so00"}],"body":{"attachmentId":"ANGjdJ-6dRpb4OteV9kWWbffdzDBmaG-9uVd8HR3Ws7SmkrmxDN3x4kFxOzoZ7g_fHiEyTYxr4ntUDr9BkKd3ID4n4PXW9kCIdbJW_YxKPLF_9qo3Nkb9pphENvrk-sIt_uw7oB2GugjahjGclTdA4o4OmD99mWQ_A7a-b3ltDQc3qiek8KrhJ-lEi8nNo3PGu1PdRqLpNWqz3wgPE12NuhZRJngjNKOuymBw_DzSieO9qBKTNYHgVHyI0n7CQCTbT5C89Brdg3zsY_mQcME5EzzXc2AKMRvqj--pX_FSrEcvWez3aKrm0HJxkgWciQ","size":238}},{"partId":"3","mimeType":"image/jpeg","filename":"Black.jpg","headers":[{"name":"Content-Type","value":"image/jpeg; name=\"Black.jpg\""},{"name":"Content-Disposition","value":"attachment; filename=\"Black.jpg\""},{"name":"Content-Transfer-Encoding","value":"base64"},{"name":"X-Attachment-Id","value":"f_iqmc2wi72"}],"body":{"attachmentId":"ANGjdJ8vs2lX1yfy9JPaxb7Ugy1wROlbSMCwgJRsWrNeKlDDdVrIpePkfreijftYoi5uQGHAFYIyblwcDfFH4v9vuNXAbT-JpozA9RFHtpHdsIq-98wp7sW6hvqirNphvzzCD7TCg4fG-emzxsqPFvQF6Oc85EEZ0in7EMagtunwkd-z9VAcqzQd-i3u8134B7y4YiY_lDI2gtnjv6ioVJONbDFzYiAVxELEVuvNFJwlb4ZrCdAVExA4OZO0Xqgjvm1RfsiPNiHu3PNbXhX4lrdJLKImmdDkIpQMU2CbOjmiSyPcxy47St4mC1USr68","size":74292}}]},"sizeEstimate":76042,"result":{"id":"155e987feb707c32","threadId":"155e987feb707c32","labelIds":["SENT","INBOX","IMPORTANT"],"snippet":"Test for getting attachments in Expert.","historyId":"873165","internalDate":"1468501851000","payload":{"mimeType":"multipart/mixed","filename":"","headers":[{"name":"MIME-Version","value":"1.0"},{"name":"Received","value":"by 10.237.32.135 with HTTP; Thu, 14 Jul 2016 06:10:51 -0700 (PDT)"},{"name":"Date","value":"Thu, 14 Jul 2016 14:10:51 +0100"},{"name":"Delivered-To","value":"email@address.co.uk"},{"name":"Message-ID","value":"<CAJvKzxJXb4ERig_Lj9PE7mFNDZLMK6cuKpa0uKba5f1KRvE-=A@mail.gmail.com>"},{"name":"Subject","value":"AttachmentTest"},{"name":"From","value":"Sender <email@address.co.uk>"},{"name":"To","value":"Receiver <email@address.co.uk>"},{"name":"Content-Type","value":"multipart/mixed; boundary=001a113e81f03989b70537983b20"}],"body":{"size":0},"parts":[{"mimeType":"multipart/alternative","filename":"","headers":[{"name":"Content-Type","value":"multipart/alternative; boundary=001a113e81f03989aa0537983b1e"}],"body":{"size":0},"parts":[{"partId":"0.0","mimeType":"text/plain","filename":"","headers":[{"name":"Content-Type","value":"text/plain; charset=UTF-8"}],"body":{"size":41,"data":"VGVzdCBmb3IgZ2V0dGluZyBhdHRhY2htZW50cyBpbiBFeHBlcnQuDQo="}},{"partId":"0.1","mimeType":"text/html","filename":"","headers":[{"name":"Content-Type","value":"text/html; charset=UTF-8"}],"body":{"size":62,"data":"PGRpdiBkaXI9Imx0ciI-VGVzdCBmb3IgZ2V0dGluZyBhdHRhY2htZW50cyBpbiBFeHBlcnQuPC9kaXY-DQo="}}]},{"partId":"1","mimeType":"text/plain","filename":"text.txt","headers":[{"name":"Content-Type","value":"text/plain; charset=US-ASCII; name=\"text.txt\""},{"name":"Content-Disposition","value":"attachment; filename=\"text.txt\""},{"name":"Content-Transfer-Encoding","value":"base64"},{"name":"X-Attachment-Id","value":"f_iqmc263q1"}],"body":{"attachmentId":"ANGjdJ-R1qc49sxpIHWRJpj6_zPF70sshHhNsKvCAqxHkYzltyxfwme_or6vK069GaKpDJzjHrtVsZqtQkHjTUSzSYg5yUOqM5y618mmOOIJUL_NnnOvrmTWVh0fKfMY1FAMGy1RJeUkb7vrbdv6lYXnAHYwojPMVx1aIG6DPWqqieQdBdE9p0704yxvbfoUu9XjeueV1FRs4b1B7x9mcS8Jj_7-uKo7a4l9nVr2HfrdnL4RS8D1JwHe1-DZiF4xiEX2j4Gre-36FooEcOnuZ_1RQryecIh5Fq_kobtFco-4_jd-w8gIMZYSHVDzUiw","size":12}},{"partId":"2","mimeType":"image/png","filename":"White.png","headers":[{"name":"Content-Type","value":"image/png; name=\"White.png\""},{"name":"Content-Disposition","value":"attachment; filename=\"White.png\""},{"name":"Content-Transfer-Encoding","value":"base64"},{"name":"X-Attachment-Id","value":"f_iqmc1so00"}],"body":{"attachmentId":"ANGjdJ-6dRpb4OteV9kWWbffdzDBmaG-9uVd8HR3Ws7SmkrmxDN3x4kFxOzoZ7g_fHiEyTYxr4ntUDr9BkKd3ID4n4PXW9kCIdbJW_YxKPLF_9qo3Nkb9pphENvrk-sIt_uw7oB2GugjahjGclTdA4o4OmD99mWQ_A7a-b3ltDQc3qiek8KrhJ-lEi8nNo3PGu1PdRqLpNWqz3wgPE12NuhZRJngjNKOuymBw_DzSieO9qBKTNYHgVHyI0n7CQCTbT5C89Brdg3zsY_mQcME5EzzXc2AKMRvqj--pX_FSrEcvWez3aKrm0HJxkgWciQ","size":238}},{"partId":"3","mimeType":"image/jpeg","filename":"Black.jpg","headers":[{"name":"Content-Type","value":"image/jpeg; name=\"Black.jpg\""},{"name":"Content-Disposition","value":"attachment; filename=\"Black.jpg\""},{"name":"Content-Transfer-Encoding","value":"base64"},{"name":"X-Attachment-Id","value":"f_iqmc2wi72"}],"body":{"attachmentId":"ANGjdJ8vs2lX1yfy9JPaxb7Ugy1wROlbSMCwgJRsWrNeKlDDdVrIpePkfreijftYoi5uQGHAFYIyblwcDfFH4v9vuNXAbT-JpozA9RFHtpHdsIq-98wp7sW6hvqirNphvzzCD7TCg4fG-emzxsqPFvQF6Oc85EEZ0in7EMagtunwkd-z9VAcqzQd-i3u8134B7y4YiY_lDI2gtnjv6ioVJONbDFzYiAVxELEVuvNFJwlb4ZrCdAVExA4OZO0Xqgjvm1RfsiPNiHu3PNbXhX4lrdJLKImmdDkIpQMU2CbOjmiSyPcxy47St4mC1USr68","size":74292}}]},"sizeEstimate":76042}}


My JSON

{"Date":"Thu, 14 Jul 2016 14:10:51 +0100","From":"Sender <email@address.co.uk>","Subject":"AttachmentTest","Reply-to":"","Body":"<div dir=\"ltr\">Test for getting attachments in Expert.</div>\r\n","ID":"155e987feb707c32","hasAttachment":"true"}


Edit2 :

This is the code that has worked for me, attachment1 is for debug purposes

$Message = {};
$Message['Date'] = getHeader(message.payload.headers, 'Date');
$Message['From'] = getHeader(message.payload.headers, 'From');
$Message['Subject'] = getHeader(message.payload.headers, 'Subject');
$Message['Reply-to'] = getHeader(message.payload.headers, 'Reply-to');
$Message['Body'] = getBody(message.payload);
$Message['ID'] = message.id;

$Message['Attachment1'] = {};
$Message['Attachment'] = {};
for (let key in message['payload']['parts']) {
if(message['payload']['parts'][key]['filename'] != "") {
$Message['Attachment1'][key] = message['payload']['parts'][key];
console.log(message['payload']['parts'][key]['filename']);
$Message['Attachment'][key] = {};
$Message['Attachment'][key]['FileName'] = message['payload']['parts'][key]['filename'];
}
}

Answer

It appears, that you are using $ in front of every variable, this is a requirement in PHP, but absolutely not required in JavaScript. Also the parenthesis () around ($LocalAttachmentCount) is not required.

Btw: {} initialises not an array but an object (by an empty object literal). But objects in JavaScript have some similarities to associative arrays in PHP, so you can use it in this way, as shown in your example.

The following code is actually running fine for me, as you can see, if you run it:

Message = {};
AttCount = 1;
while(AttCount <= 4) {
  LocalAttCount = (AttCount - 1);
  Message['Attachment'] = {};  // reset to empty Object here!
  Message['Attachment'][LocalAttCount] = {};
  Message['Attachment'][LocalAttCount]['FileName'] = 'ac' + AttCount;
  document.writeln(Message['Attachment'][LocalAttCount]['FileName']);
  document.writeln('<br>');
  AttCount++;
}

But the unintended behaviour might come from the marked line: Message['Attachment'] = {}; sets Message['Attachment'] in every loop back to an empty Object. Therefore it deletes all former [LocalAttCount]. So just move this line out of the loop, and you might get your intended behaviour:

Message = {};
Message['Attachment'] = {};  // move this line here!
AttCount = 1;
while(AttCount <= 4) {
  LocalAttCount = (AttCount - 1);
  Message['Attachment'][LocalAttCount] = {};
  Message['Attachment'][LocalAttCount]['FileName'] = 'ac' + AttCount;
  for (let key in Message['Attachment']) {
    if (Message['Attachment'].hasOwnProperty(key)) {
      document.writeln(Message['Attachment'][key]['FileName']);
    }
  }
  document.writeln('<br>');
  AttCount++;
}

Comments