2014-11-03 158 views
1

我有一个函数可以从Managed Metadata Service中获取条款。一次调用函数时一切正常。但如果我多次打电话。它给出致命的错误“该集合尚未初始化...”实际上,我使用单页应用程序,它只在我开始打开网站时才运行。它花了我一整天的时间,我需要在页面中调用该功能5-6次。任何人都可以帮忙我的代码有什么问题?executeQueryAsync在SharePoint应用程序中无法正常工作

var context = new SP.ClientContext(spContext.hostWeb.appWebUrl); 
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context); 
var termStores = taxSession.get_termStores(); 
var termStore = termStores.getByName("Yönetilen Meta Veri Hizmeti"); 
var termSet = termStore.getTermSet(termsetguid); 
var terms = termSet.getAllTerms(); 
context.load(terms); 
context.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed)); 

function onQuerySucceeded(sender, args) { 
try { 
    var termEnumerator = terms.getEnumerator(); 
    var termList = []; 
    while (termEnumerator.moveNext()) { 
     var currentTerm = termEnumerator.get_current(); 
     termList.push(currentTerm.get_name()); 
    }} 
    catch (e) { 
     common.logger.logWarning("Warning", "" , true); 
    } 
} 
function onQueryFailed(sender, args) { 
common.logger.logError("", "Error", true); 
} 

感谢

回答

1

这个错误通常发生在下列情况:

  • 当客户对象尚未明确或含蓄地要求(terms你的情况)
  • SP.ClientContext.executeQueryAsync在调用一个循环。由于 指定的函数是异步行为可能是不可预测的

尽量使变量本地(例如,使用匿名函数括起来)

(function(){ 

    var context = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl); 
    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context); 
    var termStores = taxSession.get_termStores(); 
    var termStore = termStores.getByName(termStoreName); 
    var termSet = termStore.getTermSet(termsetGuid); 
    var terms = termSet.getAllTerms(); 
    context.load(terms); 
    context.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded),Function.createDelegate(this, onQueryFailed)); 

})(); 
+0

还是相同的情况下......在我项目我需要在一个页面中使用该功能两次或更多次。这给我错误 – 2014-11-04 07:21:23

相关问题