2

I need to add autocomplete to an input textbox. The data needs to be fetched from SharePoint using AJAX / REST.

This what I've done so far:

JS

var myData = [];
var requestHeaders = {
"accept": "application/json;odata=verbose"
}

$.ajax({
url: "https://my-URL/sites/RMA-GFPLC/_api/web/lists/GetByTitle('AD_DB')/items?    $select=Title,Regional_x0020_Office,Commodity,Commodity_x0020_Year,StateLookUp/Title&$expand=StateLookUp",
type: 'GET',
dataType: 'json',
async: false,
headers: requestHeaders,
success: function (data) {
    $.each(data.d.results, function (i, result) {
        myData.push(result.Title);

    });

    myDataSource(myData);


},
error: function ajaxError(response) {
    alert(response.status + ' ' + response.statusText);
}
});

function myDataSource(myData){
$('#myAutoCompleteSearch').autocomplete({
source: myData,
minLength: 3
});
}

So far my code is not working, and I'm getting "Uncaught TypeError: Cannot read property 'label' of null " error in my console. I;m wonder what am I doing wrong here? Thanks!

2 Answers 2

2

This error occurs when a source for Autocomplete function contains an element(s) with a null value.

Solution

Add the condition for checking if value is not null:

$.each(data.d.results, function (i, result) {
      if(result.Title) {
         myData.push(result.Title);
      }   
}); 
Sign up to request clarification or add additional context in comments.

1 Comment

Good point, because I noticed that it started working with Title, which doesn't have any null value, but it doesn't work with the rest of the fields (columns) may contain null values (empty rows). Brilliant! Thanks!
-1

Jast insert your code in

$(document).ready(function () {

//your code here }

1 Comment

This 3-year old question already has an accepted answer that seems to have solved the problem. Your answer doesn't even answer the question. Please read How to answer a question

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.