RHamilton
10/08/2024, 1:41 PMUmbraco 13.5.1
and Skybrud.Umbraco.Redirects 13.0.4
. I am getting a javascript error in the console. The error is being thrown in Dashboards/Default.js and says "r.data.items.forEach is not a function"
angular.js:15697 TypeError: r.data.items.forEach is not a function
at Default.js?d=638639903000000000:181:22
at angular.js:18075:37
at m.$digest (angular.js:19242:15)
at m.$apply (angular.js:19630:13)
at k (angular.js:13473:36)
at v (angular.js:13730:7)
at y.onload (angular.js:13635:9) 'Possibly unhandled rejection: {}'
skybrudRedirectsService.getRootNodes().then(function (r) {
r.data.items.forEach(function (rootNode) {
vm.rootNodes.push(rootNode);
});
});
Notice in the screen shot - I do have multiple root nodes
When I check in the database directly, I do see many rows have been created by our content editors
(screenshot)
Anyone have any thoughts?
https://cdn.discordapp.com/attachments/1293206600181612598/1293206600672219296/image.png?ex=67068800&is=67053680&hm=3596f94c2ba8345c52ad7d0be01c8213af3ee8f6e8b1f7813bdafd59deeed0b8&
https://cdn.discordapp.com/attachments/1293206600181612598/1293206600940781660/image.png?ex=67068800&is=67053680&hm=f8875653dbbfae1483d11747fa1b8448d32d59e4a4b878c85f1ba12da95b1d00&RHamilton
10/10/2024, 5:49 PMr.data.items
is an array: In several places where the code assumes r.data.items
is an array, we added a check using Array.isArray(r.data.items)
to confirm this before iterating over it.
Fallback to $values
property if items is not an array: If r.data.items
is not an array but has a $values
property, the code now checks if r.data.items.$values
is an array and uses that instead.
Set an empty array as a default: If neither r.data.items
nor r.data.items.$values
is an array, the code safely sets the relevant variable (e.g., vm.redirects) to an empty array to prevent runtime errors.
Why These Changes Were Made:
Avoiding runtime errors: The original code assumes that r.data.items
is always an array, but if it isn't (e.g., null, undefined, or an object with $values
), trying to iterate over it would throw an error. These changes ensure the code only attempts iteration when items is actually a valid array.
Handling different data structures: Some API responses may return data structures with a $values
property (e.g., serialized collections), so the code now gracefully handles such cases.
Fail-safe implementation: By setting default empty arrays when no valid array is found, the code avoids unexpected crashes and keeps the system functioning even in edge cases.
This logic was added in a few different files, but this is what got us up and running. Happy to share code if anyone needs it. Since it has awareness of trying to use the original or alternate way of checking items it should hopefully backward compatible as well.Anders Bjerner
10/10/2024, 6:06 PM/umbraco/backoffice/Skybrud/Redirects/GetRedirects
looks like?RHamilton
10/10/2024, 7:48 PM