массив [oListItem.id] имеет значения, но массив [6], массив [7] и c. не определены - PullRequest
0 голосов
/ 17 января 2020

Я извлекаю значения из списка SharePoint и помещаю их в массив itemprices.

Использование console.log("Price of list item at is:" + itemprices[oListItem.id]); выведет значения массива, но использование console.log("itemprices 5: " + itemprices[5]); говорит мне, что они не определены :

undefined

Это код, который я использовал:

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. Что я делаю не так?

Ответы [ 2 ]

1 голос
/ 17 января 2020

Во-первых, пожалуйста, используйте правильный объект ClientContext, в приведенном выше фрагменте кода это должен быть clientContext, а не контекст, и если вы хотите заполнить значения полей в массив, попробуйте использовать array.pu sh, вот измененный фрагмент кода для вашей справки:

<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>

Это мой список данных:

enter image description here

Это массив из консоли:

enter image description here

0 голосов
/ 17 января 2020

Вы можете просто использовать эту функцию:

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...