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?
The above code produces:
Solved! Go to Solution.
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.
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.
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
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.
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?
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.