Closed huylv closed 8 years ago
It sounds like a problem with your code and not babel.
TypeError: Cannot read property 'id' of undefined
at Store.decrementInventory (ProductStore.js:41:32)
is trying to read product.id
and product
is undefined.
Thank you for your reply, Jordan. I believe I just copied everything in the tutorial until step 3. I know exactly where the error comes from, but can't explain why. Have a look at this gist and try it yourself if possible. I run it with command babel-node --presets es2015 main.js
. All files are stored in a flat structure (I've adjusted some import
paths to flatten).
Let me explain the data flow:
actions.fetchProducts()
is triggered in main.js
to send an even RECEIVE_PRODUCTS
to the reactor.receiveProducts
from ProductStore
.receiveProducts
, somehow the reactor invokes decrementInventory
instead, of course the payload is empty, thus the error.I've tried the example downloaded from this git and it works just fine. However if I change to babel-node
, things go wrong. The registered callback and the invoked callback never match. Sometimes, the invoked one is from CartStore
.
Ok, so I think I've found the answer. If I change the import
statements in ProductStore.js
and actions.js
from:
import { RECEIVE_PRODUCTS, ADD_TO_CART } from './actionTypes'
to:
import actionTypes from './actionTypes'
and use them like actionTypes.RECEIVE_PRODUCTS
, everything works. With the former directive, constants can't be resolved, so their values are undefined
. But I have no idea why...
Update: I've found the explaination for this issue on Stack Overflow. Turns out the former syntax is not quite valid in ES6 (preset es2015) so Babel 6 translates it differently. Thanks for your help.
I'm trying to replicate the tutorial on optimizely.github.io with Babel 6 (latest) as transpiler. I'm stuck at step 3:
can't seem to make it work:
Here is the list of dependencies:
Please help.