[英]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.