【发布时间】:2020-12-18 23:37:51
【问题描述】:
运行separateInformationToObjects函数时,无法读取forEach push函数内部数组的属性。
在函数separateInformationToObjects 中,数组名称(marketDataUSDPricesObjectProperties 和marketDataUSDRelativeObjectProperties)在使用时不会像其他名称一样突出显示。
我正在使用 javascript 和 nodeJS
调用函数:
separateInformationToObjects(marketDataUSD, particleDataAll,particlePriceDataAll,particleVolumeDataAll, particleMarketCapDataAll,marketDataUSDPricesObjectProperties,marketDataUSDRelativeObjectProperties)
数组属性:
let marketDataUSDRelativeObjectProperties = [
"price_change_percentage_1h_in_currency_relative_percentage",
"price_change_percentage_24h_in_currency_relative_percentage",
"price_change_percentage_7d_in_currency_relative_percentage",
"price_change_percentage_30d_in_currency_relative_percentage",
"price_change_percentage_200d_in_currency_relative_percentage",
"price_change_percentage_1y_in_currency_relative_percentage"
]
功能
function separateInformationToObjects (marketDataUSD, particleDataAll,particlePriceDataAll,particleVolumeDataAll, particleMarketCapDataAll,marketDataUSDPricesObjectProperties,marketDataUSDRelativeObjectProperties) {
//Push separate information to designated array
particlePriceDataAll.forEach((array, index) => {
marketDataUSD.forEach((coin) => {
array.push({
"id": coin.id,
"particleInfo": coin.marketDataUSDPricesObjectProperties[index],
"relativePercentage": coin.marketDataUSDRelativeObjectProperties[index]
});
});
});
}
ParticleDatAll 是一个数组数组:
let particleDataPrice1h = [],
particleDataPrice24h = [],
particleDataPrice7d = [],
particleDataPrice30d = [],
particleDataPrice200d = [],
particleDataPrice1y = [];
let particlePriceDataAll = [particleDataPrice1h, particleDataPrice24h,particleDataPrice7d ,particleDataPrice30d, particleDataPrice200d, particleDataPrice1y]
错误
(node:9003) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
at marketDataUSD.forEach ()
at Array.forEach (<anonymous>)
at particlePriceDataAll.forEach ()
at Array.forEach (<anonymous>)
at separateInformationToObjects ()
at getSparklineData.then.sparklineTimeFrameResults ()
(node:9003) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9003) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
【问题讨论】:
-
我很确定堆栈跟踪比您提供的更详细
-
coin.marketDataUSDPricesObjectProperties或coin.marketDataUSDRelativeObjectProperties均未定义。 -
数组
particlePriceDataAll里面是什么 -
@Aplet123 为什么它们没有定义?我已将它们添加到函数参数中
-
@Sam 我已经用particlePriceDataAll 的数组数据编辑了我的问题
标签: javascript node.js arrays foreach