Answer the question
In order to leave comments, you need to log in
How to get all product categories and subcategories in Opencart?
I need to get and view all the categories and subcategories of the current product.
Answer the question
In order to leave comments, you need to log in
request is asynchronous, and the value of "j" in the loop runs further than the size of the array.
The correct solution without async would be:
for (var i = 0; i < itemIds.length; i++) {
for (var j = 0; j < items.length; j++) {
if (itemIds[i] == items[j].id) {
if (i > 0) { // itemIds[-1] кинет всю ту же ошибку, потому проверяем на range
i--;
}
(function (item) {
request("http://.ru/cost.php?item=" + encodeURIComponent(item.market_hash_name), function(error, response, body) {
if (!error && response.statusCode === 200) {
if (body == "false") {
logger.warn('Не нашел цену для предмета, пропускаю');
} else {
itemsin.push({
user: res[0].name,
ava: res[0].avatar,
itemname: item.market_name,
image: item.icon_url,
color: item.name_color,
market_hash_name: item.market_hash_name,
steamid: offer.steamid_other,
cost: parseFloat(body)
});
}
}
});
})(items[j]);
}
}
}
Either you somehow copied it crookedly, or you have a problem with curly braces, namely, for (var j = 0; j < items.length; j++) {
you closed the curly brace for the loop, and then you don’t close the curly brace for for (var i = 0; i < itemIds.length; i++) {
, but );
. And then 3 more curly braces are superfluous.
Try it like this:
var i = itemIds.length;
for (var i = 0; i < itemIds.length; i++) {
for (var j = 0; j < items.length; j++) {
if (itemIds[i] == items[j].id) {
request("http://.ru/cost.php?item=" + encodeURIComponent(items[j].market_hash_name), function (error, response, body) {
i--;
if (!error && response.statusCode === 200) {
if (body == "false") {
logger.warn('Не нашел цену для предмета, пропускаю');
} else {
itemsin.push({
user: res[0].name,
ava: res[0].avatar,
itemname: items[j].market_name,
image: items[j].icon_url,
color: items[j].name_color,
market_hash_name: items[j].market_hash_name,
steamid: offer.steamid_other,
cost: parseFloat(body)
});
}
}
});
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question