2
votes

(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.

I have this error in my node.js backend when I try to login with an user. Why does it appear? Here is the line:

   if (!profilePromise.rows.length) {
            resolve({
                success: true,
                cardDetails: null,
                planId: null
            })
            return;
        }

I have problems with "length" above

1

1 Answers

1
votes

Based on the name, profilePromise is a promise. Promises don't have a rows property, so profilePromise.rows is undefined and you can't read any properties from undefined.

You need to consume the promise and use its fulfillment value, which I'm guessing is something with a length property:

profilePromise
.then(rows => {
    if (rows.length) {
        resolve(/*...*/);
    }
})
.catch(error => {
    // ...handle/report the error...
    // Probably `reject` here?
});

More about using promises here.


Side note: Assuming I'm right that profilePromise is indeed a promise, it suggests that this code is falling prey to the explicit promise creation antipattern. Instead of creating your own promise and then calling resolve or reject, chain onto the existing 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
    });