0

I am using AngularJS Bootstrap typeahead component to display data from an array of objects. The problem is that i keep on getting the following error even though my data is being return from my api call.

TypeError: Cannot read property 'length' of undefined

Here is what the controller looks like at the moment.

$scope.companySearch = function(val) {
    LeadsService.getCompany(val)
        .then(function(resp) {
            return resp.resp;
        });
};

and the directive

 <input typeahead="company as item.company_name for item in companySearch($viewValue)" id="companyName" name="company_name" type="text" class="form-control input-md" ng-model="companyDetails.company_name" typeahead-wait-ms="200" required>

Finally her is the data that im trying to fetch;

[{"id":2,"company_name":"Test Company","address1":"abc","address2":"def","po_box":"1234","city_id":2,"email":"[email protected]","phone":"9898","fax_no":9798,"website":"www.jgjh.com","corporate_code":null,"contact_person_first_name":"First","contact_person_last_name":"Last","contact_person_phone":"98989","contact_person_email":"[email protected]"},{"id":3,"company_name":"Test Company","address1":"abc","address2":"def","po_box":"1234","city_id":2,"email":"[email protected]","phone":"9898","fax_no":9798,"website":"www.jgjh.com","corporate_code":null,"contact_person_first_name":"First","contact_person_last_name":"Last","contact_person_phone":"98989","contact_person_email":"[email protected]"},{"id":4,"company_name":"Test Company","address1":"abc","address2":"def","po_box":"1234","city_id":2,"email":"[email protected]","phone":"9898","fax_no":9798,"website":"www.jgjh.com","corporate_code":"17","contact_person_first_name":"First","contact_person_last_name":"Last","contact_person_phone":"98989","contact_person_email":"[email protected]"},{"id":5,"company_name":"Test Company","address1":"abc","address2":"def","po_box":"1234","city_id":2,"email":"[email protected]","phone":"9898","fax_no":9798,"website":"www.jgjh.com","corporate_code":"17","contact_person_first_name":"First","contact_person_last_name":"Last","contact_person_phone":"98989","contact_person_email":"[email protected]"},{"id":6,"company_name":"Test Company","address1":"abc","address2":"def","po_box":"1234","city_id":2,"email":"[email protected]","phone":"9898","fax_no":9798,"website":"www.jgjh.com","corporate_code":"18","contact_person_first_name":"First","contact_person_last_name":"Last","contact_person_phone":"98989","contact_person_email":"[email protected]"},{"id":7,"company_name":"Test Company","address1":"abc","address2":"def","po_box":"1234","city_id":2,"email":"[email protected]","phone":"9898","fax_no":9798,"website":"www.jgjh.com","corporate_code":"19","contact_person_first_name":"First","contact_person_last_name":"Last","contact_person_phone":"98989","contact_person_email":"[email protected]"},{"id":8,"company_name":"Test Company","address1":"abc","address2":"def","po_box":"1234","city_id":2,"email":"[email protected]","phone":"9898","fax_no":9798,"website":"www.jgjh.com","corporate_code":"20","contact_person_first_name":"First","contact_person_last_name":"Last","contact_person_phone":"98989","contact_person_email":"[email protected]"},{"id":9,"company_name":"Test Company","address1":"abc","address2":"def","po_box":"1234","city_id":2,"email":"[email protected]","phone":"9898","fax_no":9798,"website":"www.jgjh.com","corporate_code":"21","contact_person_first_name":"First","contact_person_last_name":"Last","contact_person_phone":"98989","contact_person_email":"[email protected]"},{"id":10,"company_name":"Test Company","address1":"abc","address2":"def","po_box":"1234","city_id":2,"email":"[email protected]","phone":"9898","fax_no":9798,"website":"www.jgjh.com","corporate_code":"22","contact_person_first_name":"First","contact_person_last_name":"Last","contact_person_phone":"98989","contact_person_email":"[email protected]"},{"id":11,"company_name":"Test Company","address1":"abc","address2":"def","po_box":"1234","city_id":2,"email":"[email protected]","phone":"9898","fax_no":9798,"website":"www.jgjh.com","corporate_code":"23","contact_person_first_name":"First","contact_person_last_name":"Last","contact_person_phone":"98989","contact_person_email":"[email protected]"}]

If it helps, i get the error before my promise is returned.

1
  • companySearch doesn't return anything. Commented Jun 11, 2015 at 20:59

2 Answers 2

1

Try with this code for Typeahead asynchronous loading data

$scope.companySearch = function(val) {
    return LeadsService.getCompany(val)
        .then(function(resp) {
            return resp.data;
        });
};
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$scope.companySearch = function(val) {
    return LeadsService.getCompany(val)
        .then(function(resp) {
            return resp.resp;
        });
};

Which returns the promise directly to the typeahead directive. typeahead will wait for the data to be resolved.

1 Comment

No problem. I ran into the same issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.