TechnoCorner
TechnoCorner

Reputation: 5135

Destructuring JavaScript returns an error

I have a function and I am trying to destructure couple params from options

  fetchArtifactsWithFiltersOG = async options => {
   
    const { nextPage, fromSearch } = options;

  }

Issue:

However, there are some use cases where options are not passed, in which case it throws a nextPage of undefined error.

How do I destructure in this case where I am not sure if options is only passed sometimes. Without making my syntax ES5.

Upvotes: 0

Views: 150

Answers (3)

Martial Anouman
Martial Anouman

Reputation: 410

You should assign a default value to options:

fetchArtifactsWithFiltersOG = async (options: {}) => {

    const { nextPage, fromSearch } = options;

}

or

fetchArtifactsWithFiltersOG = async options => {

    const { nextPage, fromSearch } = options || {};

}

Upvotes: 0

wrangler
wrangler

Reputation: 3576

You can add default values:

const { nextPage, fromSearch } = options || { nextPage:null, fromSearch:null };

Upvotes: 1

Quentin
Quentin

Reputation: 943163

Set a default value for the options argument.

async (options = {}) =>

or

async (options = { nextPage: 1, frontSearch: false }) =>

Upvotes: 5

Related Questions