Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
Getting [object Object] error in Javascript and can't identify the problem

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);
    }
});

}

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!