From ab2719a1e6ece616be6854072c464eb960c8c22d Mon Sep 17 00:00:00 2001 From: Callum Kerr Date: Sat, 11 Mar 2017 10:00:10 -0700 Subject: [PATCH] Prevent extractValue from blowing up on undefined Given that angucomplete-alt supports passing a `.` separated key in `image-field` (and other fields), this fix will prevent an error in the case of an undefined value. That is, given: ``` let data = [{ // ... id: 1, logo: undefined, // ... } , { // ... id: 2, logo: { thumbnail: '/img/users/portrait.png'} } }] ``` and passing `image-field="logo.thumbnail" local-data="data"` to angucomplete-alt, this currently causes an error on the first item: `TypeError: Cannot read property 'thumbnail' of undefined`, and no further items are rendered in the autocomplete dropdown. This PR just adds a check to see if the current value exists before attempting to access the property. Came across this in the course of work, just thought I'd submit this fix for you (worked around it locally). Let me know if you need me to do more to improve the PR, but it seems fairly trivial. Regards, Callum --- angucomplete-alt.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/angucomplete-alt.js b/angucomplete-alt.js index f9fdf83d..1255e771 100644 --- a/angucomplete-alt.js +++ b/angucomplete-alt.js @@ -208,7 +208,9 @@ keys= key.split('.'); result = obj; for (var i = 0; i < keys.length; i++) { - result = result[keys[i]]; + if (result) { + result = result[keys[i]]; + } } } else {