【问题标题】:"getEnumerator is not a function" Javascript (Sharepoint Online)“getEnumerator 不是函数”Javascript (Sharepoint Online)
【发布时间】:2015-07-14 22:04:42
【问题描述】:

有谁知道我为什么会得到

“未捕获的类型错误:list.getEnumerator 不是函数”

在我的 OnSuccess() 函数中?

当我尝试获取网站集中所有列表的标题时,这段代码运行良好。

现在我想在名为testIssues 的列表中获取分配给 John Doe 的所有行的标题。

我错过了什么?

'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];
        }
    }
}

【问题讨论】:

  • 确保在 SP.js 库加载后初始化您的上下文。您应该在开发人员工具的帮助下调试您的代码并检查 list 对象中的内容(我猜它以 null 的形式出现,这可能是原因。)

标签: javascript list sharepoint get


【解决方案1】:

您正在将列表项加载到名为 listItems 的变量中,而不是 list

试试var listEnumerator = listItems.getEnumerator();

【讨论】:

    【解决方案2】:

    您的问题可能与您执行代码的时间有关,您必须确保在页面加载过程的正确时刻执行您的代码。尝试使用$(window).loadwindow.onload,然后使用SP.SOD.executeFuncExecuteOrDelayUntilScriptLoaded 确保SharePoint JS 库的加载,例如:

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

    查看这个帖子了解更多详情。

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

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 2017-07-19
      • 1970-01-01
      • 2020-10-16
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 2023-03-19
      • 2014-12-10
      相关资源
      最近更新 更多