If you can not find you question, put it as comment below. I will answer it and add it to the list.
It means that products
is null.
check the value of products
when loading = true
, It should be empty array []
Also, check the value of products
when loading = false
, It should be products array.
If you are using redux, make sure that in the reducer you set the default value for products to an empty array.
function productListReducer(state = { products: [] }, action) {
...
}
Also in ProductScreen.js
make sure you are checking loading === false && error === false
when rendering products, otherwise you will get this error.
You did not set babel correctly to transpile ES6 code to ES5. Follow this:
- npm install @babel/core, cli, node, preset-env
- Create .babelrc
{ presets: [[“@babel/preset-env”]]}
- Edit package.json
{
scripts:{
"start":"nodemon –exec babel-node server.js"
}
You did not create res.get("/api/products") on server.js or you did not set proxy. check below for proxy setting.
Check this:
- frontend/package.json
{
"name": "frontend",
"proxy": "http://127.0.0.1:5000",
...
}
- stop running frontend and backend
- Run backend first
npm start
- Then frontend
cd frontend
npm start
You need to create an api for it. add this to productRoutes.js
router.get('/categories', asyncHandler(async (req, res) => {
const categories = await Product.find().distinct('category');
res.send(categories);
}));
const product = products.find(x => x._id = props.math.params.id);
it means that either products array is empty or the param id does not exist in the array. after find method check product is not null then render it.
Edit userActions.js as follow:
const logout = () => (dispatch) => {
Cookie.remove("userInfo");
Cookie.remove("cartItems");
dispatch({ type: USER_LOGOUT })
}
re-login. it fixes the error if you had a successful login before. it is because the token expires in 48h. you can change it in utils.js
Move router.get("/mine") before router.get("/:id") in orderRoutes.js
Make sure in frontend you supply token in ajax request
Authorization: `Bearer ${token}`
Also in backend utils.js you implemented isAuth and isAdmin like course source code and in the productRoute.js you have this order:
router.post("/", isAuth, isAdmin, (req, res) => {
You didn't install mongodb. Install it from here
You didn't install mongodb. Install it from here
You need to change the email in userRoutes.js method createAdmin to create a new admin.
sandbox.paypal.com is a copy of paypal.com but you can only login here with sandbox accounts you created on the developer portal. More info here
go to the frontend folder and delete the .git folder inside that. it should work.
In windows use this:
rm -rf` with `rmdir /s /q
because it applies coding rules which your code consistent and elegant.
you need to run
npm run build
then it works.
- Create git repository
$ git init
- Create Heroku Account and install Heroku CLI
- Login on terminal and create app
$ heroku login
$ heroku apps:create amazonaapp
- Edit package.json
script: {
"build": "rm -rf dist && babel backend -d dist",
"heroku-postbuild": "npm run build && cd frontend && npm install && npm run build"
},
"engines": { "node": "12.4.0", "npm": "6.9.0"}
- Create Procfile
web: node dist/server.js
- Set Cloud MongoDB connection string
$ heroku config:set MONGODB_URL=<cloud mongodb connection string>
- Edit config.js
PORT: process.env.PORT || 5000
- Edit server.js
app.use(express.static(path.join(__dirname, '/../frontend/build')));
app.get('*', (req, res) => res.sendFile(path.join('${__dirname}/../frontend/build/index.html'));
app.listen(config.PORT, …)
- Edit .babelrc
[
"@babel/preset-env",
{"targets": {"node": "current"}}
]
- Commit and push
$ git add . && git commit -m "publish"
$ git push heroku
- Create Cloud MongoDB Account
- Open cloud.dashboard.com
- Left sidebar -> Select Security -> Database Access
- Select Add New User button
- Enter user name and password and click Add User
- Left sidebar > Select Security > Network Access
- Select Add IP Address
- Enter 0.0.0.0/0 in Whitelist Entry and click Confirm
- Left sidebar > Cluster > collections
- Create new database, set database name and collection name
- Get connection string
- Left sidebar > Select Altas > Cluster
- Click Connect
- Click Connect to your application
- Click Copy button
- Paste it in your text editor
- Replace and with step 5, and with step 10
- Use it connection string to publish on heroku (step 6)
Follow Heroku deploy steps to fix this error.
Change your command in package.json to
"scripts": {
"build": "rm -rf dist && babel backend -d dist",
...
If it does not work you can simply clone the course repo:
https://github.com/basir/node-react-ecommerce
Then follow this guide to run it locally:
https://github.com/basir/node-react-ecommerce#run-locally
Then compare your code with this.
- clean your cookie
- cast selected value in select box for QTY to number. use this: Number(e.traget.value)
- Test it again
to fix this issue you need to close all instances of nodejs in process manager in your os. then restart VS Code and it should be fixed.
You didn't install and run MongoDB correctly. Install it from https://docs.mongodb.com/manual/administration/install-community/
How we can connect backend(data.js) with frontend(e. g. src\screens\ProductScreen.js)? Because when I delete this lines:
import data from '../data';
...
const product = data.products.find(x=>parseInt(x._id) === parseInt(props.match.params.id))
console.log(props.match.params.image);
from ProductScreen.js,
gives an error message like this : Line 31:29: 'product' is not defined no-undef