I have a function below that when used on a webpage is causing the [object Object] error to occur and I was hoping someone could identify the issue for me. I am using this code to get the count of a items in particular SharePoint views so that I can display them in a dashboard for the users. Any suggestions appreciated.
function countViewItems(listTitle, viewName, elementClass) {
// Counts the number of items of in the view 'viewName' of the list 'listTitle'.
//get view query of the list view
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('"+listTitle+"')/views/getbytitle('"+viewName+"')/ViewQuery",
type: "GET",
headers: {
"ACCEPT": "application/json;odata=verbose",
},
success: function (data) {
var viewQuery=data.d.ViewQuery;
//get item count of view
var viewXml = '<View><Query>' + viewQuery + '</Query></View>';
var queryPayload = {
'query' : {
'__metadata': { 'type': 'SP.CamlQuery' },
'ViewXml' : viewXml
}
};
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems",
type: "POST",
data: JSON.stringify(queryPayload),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"Content-Type": "application/json; odata=verbose"
},
success: function (data) {
var itemCount=data.d.results.length;
$(elementClass).html(itemCount);
},
error: function (data) {
alert(JSON.stringify(data));
}
});
},
error: function (err) {
alert(err);
}
});
}