0
votes

My code :


export const convertToTickerObject = (data) => {
  const keys = [
    'id',
    'last',
    'lowestAsk',
    'highestBid',
    'percentChange',
    'baseVolume',
    'quoteVolume',
    'isFrozen',
    'high24hr',
    'low24hr'
  ];

  const object = {};
  data.forEach((value, i) => {
    if (i === 0) {
      object.name = getCurrencyPairId(value);
      return; //escape arrow function
    }
    const key = keys[i];
    object[key] = value;
  });

  return object;
};

Error:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined 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:7188) [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.

I try use async/await, but this warning continuously occured.

How can I solve this warning ? :(

1

1 Answers

0
votes

As it says in the error, the parameter you pass is probably undefined or not an object. Try this:

const convertToTickerObject = (data) => {
  if ( typeof data !== 'object' ){
    return 'Not object. Parameter type: '+ typeof data;
  }
  if( Object.keys(data).length === 0 && data.constructor === Object ){
    return 'object empty';
  }
  const keys = [
    'id',
    'last',
    'lowestAsk',
    'highestBid',
    'percentChange',
    'baseVolume',
    'quoteVolume',
    'isFrozen',
    'high24hr',
    'low24hr'
  ];

  const object = {};
  data.forEach((value, i) => {
    if (i === 0) {
      object.name = getCurrencyPairId(value);
      return; //escape arrow function
    }
    const key = keys[i];
    object[key] = value;
  });

  return object;
};

This may reveal where a mistake somewhere else. It would be good to have checks like the ones I added in place anyway.