Javascript “更新分类法”字段时引发异常;卡斯托分类场“;

Javascript “更新分类法”字段时引发异常;卡斯托分类场“;,javascript,taxonomy,sharepoint-online,sharepoint-jsom,Javascript,Taxonomy,Sharepoint Online,Sharepoint Jsom,我有一个包含分类字段的列表。我需要一个函数来更新该列表中的项目(我有id),并将该字段更改为其值之一(始终相同)。 我搜索并使用了过去使用过的函数这是我现在使用的代码: function SetSingleTaxonomyField(siteUrl, listName, fieldInternalName, itemId, term) { return new Promise(function (resolve, reject) { var context

我有一个包含分类字段的列表。我需要一个函数来更新该列表中的项目(我有id),并将该字段更改为其值之一(始终相同)。 我搜索并使用了过去使用过的函数这是我现在使用的代码:

function SetSingleTaxonomyField(siteUrl, listName, fieldInternalName, itemId, term) {
        return new Promise(function (resolve, reject) {
            var context = new SP.ClientContext(siteUrl);
            var list = context.get_web().get_lists().getByTitle(listName);
            var listItem = list.getItemById(itemId);
            context.load(listItem);

            var categoryField = list.get_fields().getByInternalNameOrTitle(fieldInternalName);
            var taxonomyValue = set_taxonomyField(term);
            var taxField = context.castTo(categoryField, SP.Taxonomy.TaxonomyField);
            taxField.setFieldValueByValue(listItem, taxonomyValue);
            listItem.update();
            context.load(listItem);
            context.executeQueryAsync(function () {
                resolve();
                //console.log('Field successfully updated.');
            }, function (sender, args) {
                reject('An error occurred:' + args.get_message());
                //console.log('An error occurred:' + args.get_message());
            });
        });
    }
上一行:

var taxonomyValue = set_taxonomyField(term);
调用此函数:

        function set_taxonomyField(term) {
        var taxonomyValue = new SP.Taxonomy.TaxonomyFieldValue();
        if (term !== undefined) {
            taxonomyValue.set_label(term.Title);
            taxonomyValue.set_termGuid(term.Id);
            taxonomyValue.set_wssId(-1);
        }
        return taxonomyValue;
    }
异常在

     var taxField = context.castTo(categoryField, SP.Taxonomy.TaxonomyField);
这是我得到的错误:

Possible Unhandled Promise Rejection: Error: Sys.ArgumentException: Value does not fall within the expected range.
Parameter name: type
at Function.Error.create (ScriptResource.axd?d=N...fc8ae3:5)
at Function.Error.argument (ScriptResource....0&t=72fc8ae3:5)
at SP.ClientContext.castTo (sp.runtime.js:2)"
我还有其他类似的函数,但这是唯一一个我都“硬编码”(术语名称、字段内部名称和术语id)的函数。但是使用相同的术语检查其他函数时,这些值感觉是正确的

我不知道怎么解决这个问题,
提前感谢。

今天我遇到了与几个月前相同的错误,我终于修复了它。问题是,我在不同的地方加载了文件“SP.Taxonomy”,因此在使用SP.Taxonomy.Taxonomy字段时,它发送了一个错误的参数(不确定原因,但删除了对SP.Taxonomy的所有引用,只有一个除外)。Taxonomy修复了它。

最后使用rest api执行操作(希望使用jsom执行此操作)