1

When running the function separateInformationToObjects It can not read the property of array inside forEach push function.

In the function separateInformationToObjects the array names ( marketDataUSDPricesObjectProperties and marketDataUSDRelativeObjectProperties) do not get highlighted as it is being used, like the other names.

I'm using javascript and nodeJS

Calling the function:

        separateInformationToObjects(marketDataUSD, particleDataAll,particlePriceDataAll,particleVolumeDataAll, particleMarketCapDataAll,marketDataUSDPricesObjectProperties,marketDataUSDRelativeObjectProperties)

Array properties:

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

   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 is a array of arrays:

let particleDataPrice1h = [],
    particleDataPrice24h = [],
    particleDataPrice7d = [],
    particleDataPrice30d = [],
    particleDataPrice200d = [],
    particleDataPrice1y = [];

let particlePriceDataAll = [particleDataPrice1h, particleDataPrice24h,particleDataPrice7d ,particleDataPrice30d, particleDataPrice200d, particleDataPrice1y]

Error

(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.
7
  • 1
    Im pretty sure that stack trace was more verbose than you are providing Commented Aug 30, 2020 at 18:34
  • 3
    Either coin.marketDataUSDPricesObjectProperties or coin.marketDataUSDRelativeObjectProperties are undefined. Commented Aug 30, 2020 at 18:35
  • What is inside the array particlePriceDataAll Commented Aug 30, 2020 at 18:35
  • @Aplet123 Why are they undefined? I have added them into the function parameters Commented Aug 30, 2020 at 18:40
  • @Sam I have edited my question with the array data of particlePriceDataAll Commented Aug 30, 2020 at 18:41

1 Answer 1

1

In the function code at line 7 & 8 you need to do coin[marketDataUSDPricesObjectProperties[index]] and coin[marketDataUSDRelativeObjectProperties[index]].

Example: you want to add the attribute hello with the value of world to the obj variable, by using x.

Don't do this:

var obj = {}, x = 'hello';
obj.x = 'world';

console.log(obj);

// Output: { x: 'hello' }

Do this:

var obj = {}, x = 'hello';
obj[x] = 'world';

console.log(obj);

// Output: { hello: 'world' }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.