ArcGIS JS Api 4.9 attachments not displaying

1092
5
Jump to solution
11-05-2018 11:06 AM
PaulGiard
New Contributor III

I have a Typescript SPA using ArcGIS JS Api 4.9.  When I query the layer for attachments I receive a attachment info array with the correct values.  When I try and display the items in the array I receive undefined?

if (layer.capabilities.data.supportsAttachment) {

const attachmentQuery = {
objectIds: feature.getAttribute('OBJECTID')
};

layer.queryAttachments(attachmentQuery).then((attachmentInfos: esri.AttachmentInfo[]) => {
console.log('Attachment infos: ', attachmentInfos);

console.log('Attachment 0: ', attachmentInfos[0]);

for (let index = 0; index < attachmentInfos.length; index++) {
const element = attachmentInfos[index];

console.log('Element: ', element);

}

attachmentInfos.forEach((attachment) => {
console.log('Attachment: ', attachment.contentType);

});
});
}

The above code produces:

  1. Attachment infos: [object Object]
    1. [object Object]: {3: Array}
      3: Array
      0: Object
      contentType: "application/pdf"
      globalId: "{108EB461-CA2B-4F31-8E59-3F20E32D3712}"
      id: 1
      name: "141 Printer map.pdf"
      parentGlobalId: "{EEED886E-3700-47DF-98F6-341506BD33E0}"
      parentObjectId: 3
      size: 2373144
      __proto__: Object
      length: "1"
      __proto__: Object
  2. Attachment 0: undefined
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
UndralBatsukh
Esri Regular Contributor

Oh sorry. I updated the app to use version 4.9. Here it is FeatureLayer.queryAttachments - 4.9 . 

foreach wont work because queryAttachments does not return array. It returns an object containing AttachmentInfo objects.

View solution in original post

0 Kudos
5 Replies
UndralBatsukh
Esri Regular Contributor

Hi there, 

You are running into that issue because FeatureLayer.queryAttachments return type is wrongly documented. It does not return an array on AttachmentInfos. Instead it returns object containing AttachmentInfos grouped by the source feature ObjectIds. So you have to find the attachments by its objectId as shown below:

featureLayer.queryAttachments(attachmentQuery).then(function (attachments) {
   console.log("attachments:", attachments);
   console.log("length of attachments", Object.keys(attachments).length);

   attachmentQuery.objectIds.forEach(function (objectId) {
     if (attachments[objectId]) {
       var attachment = attachments[objectId];
       console.group("attachment for", objectId);
       attachment.forEach(function (item) {
        console.log("attachment id", item.id);
        console.log("content type", item.contentType);
        console.log("name", item.name);
        console.log("size", item.size);
        console.log("url", item.url);
        console.groupEnd();
       });
     }
   });
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Here is a test app that shows what is described above. 

We will update the document to show the right return type for queryAttachments method.

PaulGiard
New Contributor III

Thanks for the response Undral,

I tried this code before I posted my question.  When I use the code above the attachment does not accept the forEach

I am using ArcGIS-js 4.9 Angular and Typescript, your test app is using ArcGIS 4.10

const attachment = attachments[objectId];
console.group('attachment for', objectId);

attachment.forEach(element => {
0 Kudos
UndralBatsukh
Esri Regular Contributor

Oh sorry. I updated the app to use version 4.9. Here it is FeatureLayer.queryAttachments - 4.9 . 

foreach wont work because queryAttachments does not return array. It returns an object containing AttachmentInfo objects.

0 Kudos
PaulGiard
New Contributor III

Thanks Undral,  I am able to get to the attachments now.  Will this code continue to work in 4.10?

Are Related Features queried for in the same way?

0 Kudos
UndralBatsukh
Esri Regular Contributor

Yes it will. The issue is not in the code. This issue is because the return type for FeatureLayer.queryAttachments is wrongly documented. It does not return array instead it returns an object.

0 Kudos