JS/ES6: Destructuring of undefined
You can use short circuit evaluation to supply a default if content
is a falsy value, usually undefined
or null
in this case.
const content = undefined
const { item } = content || {}
console.log(item) // undefined
How to handle destructuring of function argument with undefined in javascript?
A default value
can be used
const product = { label: 'Notebook', price: 50};
const destructSample = ({label, price}= {}) => { console.log(label, price);}
destructSample(product);destructSample(undefined);
How to destructure nested object that might be undefined in javascript
You could take a default object.
const
myFunction = () => ({}),
{ data: { key1 } = {} } = myFunction();
console.log(key1);
Why am I getting 'undefined is not a function' for this destructuring assignment code?
The value you are destructuring is an object, not an array. Therefore you need to use the object destructuring syntax. If you use the wrong flavor you get the not very helpful "undefined is not a function" error.
const {p = 1, t = 25, ...otherParams} = request.query
I struggled with this very same error last Friday.
Related Topics
Angularjs Routing 404 Error with HTML5 Mode
How Do Cors and Access-Control-Allow-Headers Work
How to Auto-Reload Files in Node.Js
How to Pass a Flag to Gulp to Have It Run Tasks in Different Ways
Angularjs - UI Router - Programmatically Add States
Differencebetween Decodeuricomponent and Decodeuri
Dynamic Require in Requirejs, Getting "Module Name Has Not Been Loaded Yet for Context" Error
Omitting the Second Expression When Using the If-Else Shorthand
JavaScript Call Nested Function
Access 'Data-' Attribute Without Jquery
How to Generate Sourcemaps When Using Babel and Webpack
How to Send Authorization Header with Axios
How to Distinguish Mouse "Click" and "Drag"
What Is the 'Execution Context' in JavaScript Exactly
What's Wrong with Var X = New Array();
Authorization of Google Drive Using JavaScript
Why Does ('0' ? 'A':'B') Behave Different Than ('0' == True ? 'A':'B')