1

I need to declare the orderItems inside a nested object in my React app. Sometimes order is null so orderItems is not found. So the error is of course Cannot read property of orderItems of null. How do I fix this?

 const { order: { orderItems = [] } = {} } = res.cartFind

1 Answer 1

1

Sometimes order is null so orderItems is not found.

Since it's null (not undefined), we can't work around it with defaults (since they only apply to things that are missing or undefined, specifically). But since it's at the top level of res.cartFind, we can work around it with nullish coalescing. Remove the {order: ....} part and move .order to the end, then default it with ?? {}, and destructure that:

const { orderItems = [] } = res.cartFind.order ?? {};

Live Example:

function example(res) {
    const { orderItems = [] } = res.cartFind.order ?? {};
    console.log(orderItems);
}

console.log("When order is null:");
example({
    cartFind: {
        order: null,
    },
});

console.log("When order is not null:");
example({
    cartFind: {
        order: {
            orderItems: [1, 2, 3],
        },
    },
});

In comments I think you've asked what if order is there and it has orderItems: null in it. With the above, you'd end up with null, because (again) defaults only get used when something is missing or undefined.

The two things that handle both undefined and null (we call those "nullish" values) are nullish coalescing (which we used above), ??, and optional chaining, ?..

If order may be missing, or it may be present but with orderItems: null in it, you can handle that with a combination of the two:

const orderItems = res.cartFind.order?.orderItems ?? [];

Here's how that works:

  1. If res.cartFind.order is null or undefined (or missing,which is effectively undefined), then res.cartFind.order?.orderItems results in undefined (it doesn't throw an error the way it would if there weren't a ? after order). If res.cartFind.order isn't null or undefined (nullish), then we try to get orderItems from it.
  2. So now res.cartFind.order?.orderItems has been evaluated and given us either undefined or the value of orderItems (which may be anything). By using thatResultValue ?? [] we're saying "use thatResultValue if it isn't null or undefined, but use [] if it is."

Live Example:

function example(res) {
    const orderItems = res.cartFind.order?.orderItems ?? [];
    console.log(orderItems);
}

console.log("When order is null:");
example({
    cartFind: {
        order: null,
    },
});

console.log("When order is not null and has a valid orderItems:");
example({
    cartFind: {
        order: {
            orderItems: [1, 2, 3],
        },
    },
});

console.log("When order is not null but doesn't have orderItems:");
example({
    cartFind: {
        order: {
        },
    },
});

console.log("When order is not null and has orderItems but it's null:");
example({
    cartFind: {
        order: {
            orderItems: null,
        },
    },
});
.as-console-wrapper {
    max-height: 100% !important;
}

Sign up to request clarification or add additional context in comments.

13 Comments

if any variable found in the future inside res.cartFind.order that is null would be automatically be fine, right?
@Joseph - I'm afraid I don't understand the question.
@Joseph With { cartFind: { order: { orderItems: null } } } you will end up with const orderItems = null as well, not an empty array. If you need to prevent that, you cannot use destructuring with default values, I recommend const orderItems = res.cartFind.order?.orderItems ?? [];.
@Joseph - See the end of Bergi's comment here, they've already shown you how to handle that with a combination of nullish coalescing and optional chaining.
@Joseph See above. You'll need two statements, one to destructure const {loading, error, data} = useQuery(…);, the other to safely access orderItems from the data.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.