错误:集合尚未初始化

时间:2015-09-24 01:27:10

标签: javascript sharepoint

我正在使用以下代码查询托管元数据的内容。我遇到了错误

  

该集合尚未初始化

当我在var level2TermsEnum = level2Terms.getEnumerator();

我已经读到这是因为JavaScript的推迟和承诺,我似乎无法理解它。也许你可以帮我透露一下。

    $(document).ready(function () {

        var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";

        $.getScript(scriptbase + "SP.Runtime.js",

        function () {

            $.getScript(scriptbase + "SP.js", function () {

                $.getScript(scriptbase + "SP.Taxonomy.js", function () {

                    context = SP.ClientContext.get_current();
                    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);

                    //Term Stores
                    var termStores = taxSession.get_termStores();
                    //Term Store under which to create the group.
                    var termStore = termStores.getByName("Managed Metadata Service");
                    var termSet = termStore.getTermSet("7b6ee52v-3709-4181-a14d-b953f2ad0aad");
                    //Call your code here.

                    GetTermsFromTaxonomyStore();
                });

            });

        });

        var json = "";
        function GetTermsFromTaxonomyStore() {

            //Current Context
            var context = SP.ClientContext.get_current();
            //Current Taxonomy Session
            var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
            var termStores = taxSession.get_termStores();
            //Term Store under which to create the term.
            //var termStore = taxSession.getDefaultSiteCollectionTermStore();
            var termStore = termStores.getByName("Managed Metadata Service");
            //Pass ID of the Meetings Term Set
            var termSet = termStore.getTermSet("7b6ee52v-3709-4181-a14d-b953f2ad0aad");
            var terms = termSet.get_terms();
            context.load(terms);
            context.executeQueryAsync(function () {
                var level1Terms = terms.getEnumerator();
                while (level1Terms.moveNext()) { //iterate thru Level1
                    var level1 = level1Terms.get_current();
                    if (level1.get_termsCount() > 0) { //check if Level1 has child
                        var level2Terms = level1.get_terms(); //get level2 terms of level 1 term
                        **var level2TermsEnum = level2Terms.getEnumerator();**
                        while (level2TermsEnum.moveNext())
                        {
                            var level2Term = level2TermsEnum.get_current();
                            var level2TermName = level2Term.get_name();
                            termsList += '"Level1":"' + level2Term.get_name() + '","Level2":"' + level2TermName + '"';
                        }

                    }
                    //console.log(currentTerm.get_name());
                }
                alert(termsList);

            }, function (sender, args) {
                console.log(args.get_message());
            });
        }

1 个答案:

答案 0 :(得分:0)

当客户端对象模型为您返回一组对象时,如果每个对象都有自己的子集合,则除非您明确要求加载它们,否则不会对它们进行初始化。

here提供的答案类似,您应该能够使用附加参数更新您对context.load()的调用,并告知其要加载的内容。

我相信下面的代码会起作用,但我还没有测试过它:

context.load(terms,"Include(Name,Terms,Terms.Include(Name))");