“getEnumerator is not a function” Javascript (Shar

2019-07-27 02:59发布

问题:

Does anyone know why I get

"Uncaught TypeError: list.getEnumerator is not a function"

in my OnSuccess() function?

This code worked fine before, when I tried to get the Titles of all lists in my site collection.

Now I want to get the titles of all rows assigned to John Doe, in my list called testIssues.

What have I missed?

'use strict';
var clientContext = new SP.ClientContext.get_current();
var hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var parentContext = new SP.AppContextSite(clientContext, hostweburl);
var parentWeb = parentContext.get_web();
var list = parentWeb.get_lists().getByTitle("testIssues");
var listItems;

$(document).ready(function () {

});

function VisaLista() {
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query><Where><Geq><FieldRef Name='p32c'/>" +
    "<Value Type='User'>John doe</Value></Geq></Where></Query></View>");
    listItems = list.getItems(camlQuery);
    clientContext.load(listItems);
    clientContext.executeQueryAsync(OnSuccess, OnFail);
}

function OnSuccess() {
    var listString;
    var listEnumerator = list.getEnumerator();
    while (listEnumerator.moveNext()) {
        var currentItem = listEnumerator.get_current();
        listString += "<br/> " + currentItem.get_title();
    }
    $('#divAllaListor').html(listString);
}

function OnFail(sender, args) {
    alert('Failed, Error:' + args.get_message());
}

function getQueryStringParameter(param) {
    var params = document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == param) {
            return singleParam[1];
        }
    }
}

回答1:

You're loading the list items into a variable named listItems, not list.

Try var listEnumerator = listItems.getEnumerator();



回答2:

Your problem may be related with the time when you execute your code, you must ensure to execute your code at the right moment of the page load process. Try using $(window).load or window.onload and then ensure the load of SharePoint JS libraries by using SP.SOD.executeFunc or ExecuteOrDelayUntilScriptLoaded, by example:

$(window).load(function(){
     SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function (){
          var clientContext = new SP.ClientContext.get_current();
          var hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
          var parentContext = new SP.AppContextSite(clientContext, hostweburl);
          var parentWeb = parentContext.get_web();
          var list = parentWeb.get_lists().getByTitle("testIssues");
          var listItems;
          //Call your function here
          VisaLista();
     });
});

Take a look at this thread for more details.

Using jQuery / javascript How to check if JS file ( SP.JS) is already called in a page?