【问题标题】:array[oListItem.id] has values but array[6], array[7] etc. are undefinedarray[oListItem.id] 有值,但 array[6]、array[7] 等未定义
【发布时间】:2020-05-03 18:57:49
【问题描述】:

我从 SharePoint 列表中检索值并将它们放入数组 itemprices

使用console.log("Price of list item at is:" + itemprices[oListItem.id]); 将打印出数组的值,但使用console.log("itemprices 5: " + itemprices[5]); 告诉我它们是未定义的:

这是我使用的代码:

var itemprices = [];

// Gets values from Catalogue list; but they can't be used in the Position list because of different formats
function retrieveListItems() {
    var clientContext = new SP.ClientContext.get_current();
    catalogueList = clientContext.get_web().get_lists().getByTitle('Catalog');

    var camlQuery = new SP.CamlQuery(); // initiate the query object
    camlQuery.set_viewXml('<View><Query><Where><In><FieldRef Name=\'ID\'/><Values><Value Type=\'Number\'>5</Value><Value Type=\'Number\'>6</Value><Value Type=\'Number\'>7</Value></Values></In></Where></Query></View>');
    itemColl = catalogueList.getItems(camlQuery);

    // returns the item collection based on the query
    context.load(itemColl);
    context.executeQueryAsync(retrieveListItemsSuccess, retrieveListItemsFail);
}

function retrieveListItemsSuccess() {
    var listItemEnumerator = itemColl.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        itemprices[oListItem.id] = oListItem.get_item('Preis');
        console.log("itemprices 5: " + itemprices[5]);
        console.log("itemprices 6: " + itemprices[6]);
        console.log("itemprices 7: " + itemprices[7]);
        console.log("Price of list item at is:" + itemprices[oListItem.id]);
    }
}
// This function is executed if the above call fails
function retrieveListItemsFail(sender, args) {
    alert('Failed to get list items. Error:' + args.get_message());
}

我不知道这是 JavaScript 问题还是 SharePoint 问题。我做错了什么?

【问题讨论】:

  • 运行这个会得到什么?:console.log(oListItem.id, ' is type ', typeof oListItem.id)
  • undefined " is type " "undefined"
  • 好吧,你去吧:id实际上是未定义的,所以你用键undefined在数组上设置值,而不是数字键:)
  • @IceMetalPunk 哦,我明白了。所以我应该定义一个var i = 0 并将列表项分配给i 位置的数组,然后用i++; 递增它?
  • 这可行,但它的行为与您目前似乎正在尝试做的行为不同。您应该首先找出为什么列表项的 id 属性未定义并修复它:)

标签: javascript arrays list sharepoint


【解决方案1】:

首先,请使用正确的ClientContext对象,在上面的代码sn-p中,它应该是clientContext而不是context,如果你想将字段值填充到数组中,请尝试使用array.push,这里是修改后的代码 sn-p 供您参考:

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(retrieveListItems, 'sp.js');
var itemprices = [];
// Gets values from Catalogue list; but they can't be used in the Position list because of different formats
function retrieveListItems() {
    var clientContext = new SP.ClientContext.get_current();
    catalogueList = clientContext.get_web().get_lists().getByTitle('Companies');

    var camlQuery = new SP.CamlQuery(); // initiate the query object
    camlQuery.set_viewXml('<View><Query><Where><In><FieldRef Name=\'ID\'/><Values><Value Type=\'Number\'>5</Value><Value Type=\'Number\'>6</Value><Value Type=\'Number\'>7</Value></Values></In></Where></Query></View>');
    itemColl = catalogueList.getItems(camlQuery);

    // returns the item collection based on the query
    clientContext.load(itemColl);
    clientContext.executeQueryAsync(retrieveListItemsSuccess, retrieveListItemsFail);
}

function retrieveListItemsSuccess() {
    var listItemEnumerator = itemColl.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        itemprices.push(oListItem.get_item('Title'));
      }
      console.log(itemprices);
 }
// This function is executed if the above call fails
function retrieveListItemsFail(sender, args) {
    alert('Failed to get list items. Error:' + args.get_message());
}
</script>

这是我的列表数据:

这是来自控制台的数组:

【讨论】:

    【解决方案2】:

    你可以使用这个功能:

    itemprices.map(itemPrice => console.log(itemPrice))

    【讨论】:

    • 为什么要将项目映射到console.log 的(始终未定义的)结果?
    猜你喜欢
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2016-05-08
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多