0

I have a problem with Node.js. I have already installed nodemoden and express. I got an error as the title. Would anyone take a look at my code please? I have 1 index.js file and 3 json files. Also, I have 6 js files, 1 is meals.js below. Other 5 files are cheapmealsroute.js. largemealsroute.js and so on.

index.js

const express = require('express');
const app = express();
const port = 3001;

const meals = require('./data/meals.json');
const reservations = require('./data/reservations.json');
const reviews = require('./data/reviews.json');

const mealsRouter = require('./routes/mealsroute');
const cheapMealsRouter = require('./routes/cheapmealsroute');
const largeMealsRouter = require('./routes/largemealsroute');
const randomMealRouter = require('./routes/mealroute');
const reservationsRouter = require('./routes/reservationsroute');
const reservationRouter = require('./routes/reservationroute');


app.use('/meals', mealsRouter);

app.use('/cheap-meals', cheapMealsRouter);

app.use('/large-meals', largeMealsRouter);

app.use('/random-meals', randomMealRouter);

app.use('/reservations', reservationsRouter);

app.use('/reservation', reservationRouter);

app.listen(
  port,
  () => console.log(`Example app listening at http://localhost:${port}`)
);

mealsroute.js

const express = require('express');
const router = express.Router();

const fs = require('fs');
const meals = JSON.parseInt(fs.readFileSync(__dirname + '/../data/meals.json'));

router.get('/', (request, response) =>
  response.send(meals)

);

module.exports = router;

meals.json

[
  {
    "id": 1,
    "title": "Indian food in the summer",
    "maxNumberOfGuests": 5,
    "description": "A nice night out eating delicious indian food",
    "createdAt": "2019/12/7 14:34",
    "price": 67
  },
  {
    "id": 2,
    "title": "Italian food with best ingredients",
    "maxNumberOfGuests": 10,
    "description": "All ingredients from Italy freshly cooked",
    "createdAt": "2019/12/15 15:30",
    "price": 65
  },
  {
    "id": 3,
    "title": "Japanese food by Japanese chef",
    "maxNumberOfGuests": 4,
    "description": "A trip to Japan",
    "createdAt": "2019/11/13 10:00",
    "price": 100
  },
  {
    "id": 4,
    "title": "Danish smoere brot",
    "maxNumberOfGuests": 6,
    "description": "Taste the Danish cusine",
    "createdAt": "2019/12/8 09:26",
    "price": 70
  },
  {
    "id": 5,
    "title": "Spanish tapas",
    "maxNumberOfGuests": 8,
    "description": "Fiesta con tus amigos",
    "createdAt": "2019/12/9 16:08",
    "price": 85
  }
]
3
  • 1
    I think you meant JSON.parse, not parseInt. Commented Jun 13, 2020 at 19:39
  • As the error says, JSON.parseInt isn't a function. Maybe you meant JSON.parse Commented Jun 13, 2020 at 19:39
  • @AlexBroadwin parseInt on that meals.json file will produce NaN Commented Jun 13, 2020 at 19:50

3 Answers 3

1

Problem is that JSON.parseInt is not a function!

I don't know where you got that JSON has a parseInt function.

First parse the JSON to use the JSON as a JS Object (JSON.parse())

and then parse the integer you need with .parseInt()

Sign up to request clarification or add additional context in comments.

Comments

0

ReplaceJSON.parseInt with JSON.parse

Comments

0

If you want to convert some JSON into a JavaScript object you can use JSON.parse(myJson)

If you want to convert a string representation of an integer into a number type you can use parseInt(myStr).

I think you may be just confusing the two.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.