简体   繁体   English

UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“forEach”

[英]UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined

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. UnhandledPromiseRejectionWarning:TypeError:无法读取未定义 UnhandledPromiseRejectionWarning 的属性“forEach”:未处理的承诺拒绝。 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().这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。 (rejection id: 1) (node:7188) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. (rejection id: 1) (node:7188) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.将来,未处理的承诺拒绝将使用非零退出代码终止 Node.js 进程。

I try use async/await, but this warning continuously occured.我尝试使用 async/await,但此警告不断发生。

How can I solve this warning ?我该如何解决这个警告? :( :(

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.无论如何,像我添加的那些检查一样会很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnhandledPromiseRejectionWarning:TypeError:无法读取未定义 Discord.JS 的属性“forEach” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined Discord.JS javascript UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性'forEach' - javascript UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined UnhandledPromiseRejectionWarning TypeError:无法读取未定义的属性“属性” - UnhandledPromiseRejectionWarning TypeError: Cannot read property 'property' of undefined UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性参数 - UnhandledPromiseRejectionWarning: TypeError: Cannot read property parameter of undefined UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“名称” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of undefined UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ close” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'close' of undefined UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“数量” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'amount' of undefined UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“最高” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'highest' of undefined UnhandledPromiseRejectionWarning:类型错误:无法读取未定义的属性“播放” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'play' of undefined UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“hasPermission” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'hasPermission' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM