问题描述
我有一个奇怪的问题.它完全随机发生,我不知道它为什么以及在什么情况下出现.
I have a strange problem. It occurs totally randomly, I have no idea why and in what circumstances it comes.
详细信息:我想使用 executeQueryAsync
函数获取组的成员.在回调中 userEnumerator = users.getEnumerator();
行抛出此异常:集合尚未初始化.尚未请求或尚未执行请求.可能需要明确请求.
Details: I want to get the members of a Group with the executeQueryAsync
function. In the callback the userEnumerator = users.getEnumerator();
row throws this exception:The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
没有其他异步代码在运行.我不知道这是否重要,但只有在页面加载时运行时才会发生这种情况.
There is no other async code running. I dont know if this important, but this only happens if this is running at page load.
我使用 XML Viewer
webpart 插入此代码.
I insert this code with an XML Viewer
webpart.
var ctx = SP.ClientContext.get_current(),
groups = ctx.get_web().get_siteGroups(),
group = groups.getById(6),
users = group.get_users();
ctx.load(group);
ctx.load(users);
ctx.executeQueryAsync(function () {
var userEnumerator,
user;
$("#members-select").empty();
userEnumerator = users.getEnumerator();
while (userEnumerator.moveNext()) {
user = userEnumerator.get_current();
$("#members-select").append('<option>' + user.get_title() + '</option>');
}
});
谢谢,如果有人知道并分享有关此的任何信息.我也看到了这个问题.
Thanks, if anyone knows and shares any information about this.I saw this question as well.
推荐答案
您可以尝试稍微不同的加载组用户的方法.由于组客户端对象公开 Users 属性,您可以加载具有像这样初始化的 Users 属性的组:
You could try a slightly different approach of loading group users.Since Group client object exposes Users property, you could load Group with Users property initialized like this:
ctx.load(group,'Users');
示例:
(function(){
var ctx = SP.ClientContext.get_current();
var groups = ctx.get_web().get_siteGroups();
var group = groups.getById(6);
ctx.load(group,'Users');
ctx.executeQueryAsync(function () {
var users = group.get_users();
var e = users.getEnumerator();
while (e.moveNext()) {
var user = e.get_current();
console.log(user.get_title());
}
},
function(sender,args){
console.log(args.get_message());
}
);
})();
要点:
- 为
SP.ClientContext.executeQueryAsync
添加了错误处理程序
- Error handler was added for
SP.ClientContext.executeQueryAsync
这篇关于sharepoint javascript集合未初始化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!