I want to make a function to get data from SP list thru jsom, when i run the function with multiple items I get the error, with only one item it does work correct.
Error:
Uncaught Error: The collection was not initialized. The initialization is not requested or the application is not running. Possibly the initialisation must be made explicit.
My code: (error comes up when run go and fails on this.collListItem.getEnumerator();)
function go() {
for (var i = 0; i < 5; i++) {
getEmployee2(i);
}
}
function getData(number) {
var clientContext = new SP.ClientContext(theSite);
var oList = clientContext.get_web().get_lists().getById(emplist);
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name="Number"/><Value Type="Text">' + number + '</Value></Eq></Where></Query></View>');
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded()
{
var listItemInfo = '';
var listItemEnumerator = this.collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
console.log('emp' + oListItem.get_id());
}
}
function onQueryFailed() {
alert('fail');
}
How can I solve this? I doesn't matter if it's runs sync or async. I think collListItem is used multiple times giving the error?