Reputation: 5135
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
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
Reputation: 3576
You can add default values:
const { nextPage, fromSearch } = options || { nextPage:null, fromSearch:null };
Upvotes: 1
Reputation: 943163
Set a default value for the options argument.
async (options = {}) =>
or
async (options = { nextPage: 1, frontSearch: false }) =>
Upvotes: 5