【发布时间】: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