"getEnumerator is not a function" Javascript(Sharepoint 在线)
"getEnumerator is not a function" Javascript (Sharepoint Online)
有谁知道我为什么得到
"Uncaught TypeError: list.getEnumerator is not a function"
在我的 OnSuccess() 函数中?
这段代码之前工作正常,当我试图获取我站点中所有列表的标题时 collection。
现在我想获取分配给 John Doe 的所有行的标题,在我名为 testIssues
的列表中。
我错过了什么?
'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];
}
}
}
您的问题可能与您执行代码的时间有关,您必须确保在页面加载过程的正确时刻执行您的代码。尝试使用 $(window).load
或 window.onload
,然后使用 SP.SOD.executeFunc
或 ExecuteOrDelayUntilScriptLoaded
确保 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?
您正在将列表项加载到名为 listItems
的变量中,而不是 list
。
尝试var listEnumerator = listItems.getEnumerator();
有谁知道我为什么得到
"Uncaught TypeError: list.getEnumerator is not a function"
在我的 OnSuccess() 函数中?
这段代码之前工作正常,当我试图获取我站点中所有列表的标题时 collection。
现在我想获取分配给 John Doe 的所有行的标题,在我名为 testIssues
的列表中。
我错过了什么?
'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];
}
}
}
您的问题可能与您执行代码的时间有关,您必须确保在页面加载过程的正确时刻执行您的代码。尝试使用 $(window).load
或 window.onload
,然后使用 SP.SOD.executeFunc
或 ExecuteOrDelayUntilScriptLoaded
确保 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?
您正在将列表项加载到名为 listItems
的变量中,而不是 list
。
尝试var listEnumerator = listItems.getEnumerator();