{"action":"created","issue":{"url":"https://api.github.com/repos/select2/select2/issues/3969","labels_url":"https://api.github.com/repos/select2/select2/issues/3969/labels{/name}","comments_url":"https://api.github.com/repos/select2/select2/issues/3969/comments","events_url":"https://api.github.com/repos/select2/select2/issues/3969/events","html_url":"https://github.com/select2/select2/issues/3969","id":119353083,"number":3969,"title":"Cannot read property 'avatar_url' for templateSelection","user":{"login":"mralessio","id":16015111,"avatar_url":"https://avatars.githubusercontent.com/u/16015111?v=3","gravatar_id":"","url":"https://api.github.com/users/mralessio","html_url":"https://github.com/mralessio","followers_url":"https://api.github.com/users/mralessio/followers","following_url":"https://api.github.com/users/mralessio/following{/other_user}","gists_url":"https://api.github.com/users/mralessio/gists{/gist_id}","starred_url":"https://api.github.com/users/mralessio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mralessio/subscriptions","organizations_url":"https://api.github.com/users/mralessio/orgs","repos_url":"https://api.github.com/users/mralessio/repos","events_url":"https://api.github.com/users/mralessio/events{/privacy}","received_events_url":"https://api.github.com/users/mralessio/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-11-29T11:04:46Z","updated_at":"2015-11-29T19:02:15Z","closed_at":"2015-11-29T19:02:15Z","body":"Thank you for the plugin!\r\nI'm getting the error ```Uncaught TypeError: Cannot read property 'avatar_url' of undefined``` when trying to add 'repo.owner.avatar_url' for templateSelection, though there is no problem getting the url with templateResult as it was in your example.\r\n\r\n```\r\nfunction formatRepo (repo) {\r\n      if (repo.loading) return repo.text;\r\n      var markup = \"<div class='select2-result-repository clearfix'>\" +\r\n        \"<div class='select2-result-repository__avatar'><img src='\" + repo.owner.avatar_url + \"' /></div>\" +\r\n        \"<div class='select2-result-repository__meta'>\" +\r\n          \"<div class='select2-result-repository__title'>\" + repo.full_name + \"</div>\";\r\n      if (repo.description) {\r\n        markup += \"<div class='select2-result-repository__description'>\" + repo.description + \"</div>\";\r\n      }\r\n      markup += \"<div class='select2-result-repository__statistics'>\" +\r\n        \"<div class='select2-result-repository__forks'><i class='fa fa-flash'></i> \" + repo.forks_count + \" Forks</div>\" +\r\n        \"<div class='select2-result-repository__stargazers'><i class='fa fa-star'></i> \" + repo.stargazers_count + \" Stars</div>\" +\r\n        \"<div class='select2-result-repository__watchers'><i class='fa fa-eye'></i> \" + repo.watchers_count + \" Watchers</div>\" +\r\n      \"</div>\" +\r\n      \"</div></div>\";\r\n      return markup;\r\n    }\r\n\r\n function formatSelection(repo) {\r\n            var output = \"<img src='\" + repo.owner.avatar_url + \"' />\" + repo.full_name;\r\n            return  output || repo.text;\r\n        }\r\n    \r\n    $ajax.select2({\r\n      ajax: {\r\n        url: \"https://api.github.com/search/repositories\",\r\n        dataType: 'json',\r\n        delay: 250,\r\n        data: function (params) {\r\n          return {\r\n            q: params.term, // search term\r\n            page: params.page\r\n          };\r\n        },\r\n        processResults: function (data, params) {\r\n          params.page = params.page || 1;\r\n          return {\r\n            results: data.items,\r\n            pagination: {\r\n              more: (params.page * 30) < data.total_count\r\n            }\r\n          };\r\n        },\r\n        cache: true\r\n      },\r\n      escapeMarkup: function (markup) { return markup; },\r\n      minimumInputLength: 1,\r\n      templateResult: formatRepo,\r\n      templateSelection: formatSelection\r\n    });\r\n```"},"comment":{"url":"https://api.github.com/repos/select2/select2/issues/comments/160450331","html_url":"https://github.com/select2/select2/issues/3969#issuecomment-160450331","issue_url":"https://api.github.com/repos/select2/select2/issues/3969","id":160450331,"user":{"login":"kevin-brown","id":1991850,"avatar_url":"https://avatars.githubusercontent.com/u/1991850?v=3","gravatar_id":"","url":"https://api.github.com/users/kevin-brown","html_url":"https://github.com/kevin-brown","followers_url":"https://api.github.com/users/kevin-brown/followers","following_url":"https://api.github.com/users/kevin-brown/following{/other_user}","gists_url":"https://api.github.com/users/kevin-brown/gists{/gist_id}","starred_url":"https://api.github.com/users/kevin-brown/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kevin-brown/subscriptions","organizations_url":"https://api.github.com/users/kevin-brown/orgs","repos_url":"https://api.github.com/users/kevin-brown/repos","events_url":"https://api.github.com/users/kevin-brown/events{/privacy}","received_events_url":"https://api.github.com/users/kevin-brown/received_events","type":"User","site_admin":false},"created_at":"2015-11-29T19:02:14Z","updated_at":"2015-11-29T19:02:14Z","body":"You'll notice the special case for `|| repo.text` at the end, this is because `text` is what the property name is on the original `<option>` elements. Because there is a default value, the data objects won't have the `owner` property, so that will be `undefined` which is why you are getting the error.\r\n\r\n----\r\n\r\nThis appears to be more of a usage question than a bug report or common feature request.  As we continue to keep the Select2 issue tracker geared towards these two areas, bug reports and common feature requests, we are redirecting usage questions to other communities such as the [mailing list][ml], [IRC][irc], or [Stack Overflow][so].\r\n\r\n[ml]: https://github.com/select2/select2#mailing-list\r\n[irc]: https://github.com/select2/select2#irc-channel\r\n[so]: http://stackoverflow.com/questions/tagged/jquery-select2?sort=votes"}}