(node:55028) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined at /Users/patrickstanciu/WebstormProjects/autismassistant/backend/api/controllers/paymentsController.js:1045:34
at processTicksAndRejections (internal/process/task_queues.js:94:5) (node:55028) 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:55028) [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.
当我尝试使用用户登录时,我的 node.js 后端出现此错误。为什么会出现? 这是行:
if (!profilePromise.rows.length) {
resolve({
success: true,
cardDetails: null,
planId: null
})
return;
}
我对上面的“长度”有疑问
最佳答案
根据名称,profilePromise
是一个 promise 。 Promise 没有 rows
属性,因此 profilePromise.rows
是 undefined
并且您无法从 undefined< 读取任何属性
.
您需要使用 promise 并使用其履行值,我猜这是具有length
属性的东西:
profilePromise
.then(rows => {
if (rows.length) {
resolve(/*...*/);
}
})
.catch(error => {
// ...handle/report the error...
// Probably `reject` here?
});
有关使用 promise 的更多信息 here .
<小时/>旁注:假设我是对的,profilePromise
确实是一个 promise ,这表明这段代码正在成为 the explicit promise creation antipattern 的牺牲品。 。不要创建自己的 Promise,然后调用 resolve
或 reject
,而是链接到现有的 Promise:
return profilePromise
.then(rows => {
if (rows.length) {
return {
success: true,
cardDetails: null,
planId: null
};
}
// Either return something else here, or `throw` if `rows` not having
// a truthy `length` is an error
});
关于javascript - 未处理的PromiseRejection警告: TypeError: Cannot read property 'length' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60175036/