2014-11-03 155 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

相關問題