Skip to content

Instantly share code, notes, and snippets.

@danillouz
Created May 3, 2016 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danillouz/600b569b543ed0d13feda17ecbe9ef1c to your computer and use it in GitHub Desktop.
Save danillouz/600b569b543ed0d13feda17ecbe9ef1c to your computer and use it in GitHub Desktop.
Check yourself before you destructure yourself.
const data = {
entry: [
{
messaging: [
{
senderId: 1,
payload: {
text: 'hello'
}
}
]
}
]
};
Promise
.resolve()
.then(() => {
for (const entry of data.entry) {
for (const {
// We extract `payload` by destructuring an element from the `entry.messaging` Array,
// but because it's an Object which in turn is destructured, `:` must be used.
// Because `:` in a destructuring statement assigns to a variable with a different name
// than the (destructured) object property, i.e. `payload`, that variable is NOT defined.
payload: { text }
} of entry.messaging) {
if (payload) { // throws an error because payload is not defined
console.log(`message text: ${text}`);
}
}
}
})
.catch(err => console.log(`${err}`)); // ReferenceError: payload is not defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment