Javascript Node.js TypeError:JSON.parseInt不是函数
我对Node.js有问题。 我已经安装了nodemoden和express。 我的标题有个错误。谁能看看我的密码吗? 我有1个index.js文件和3个json文件。 另外,我有6个js文件,下面的1个是finds.js。其他5个文件是cheapmealsroute.js。largemealsroute.js等等 index.jsJavascript Node.js TypeError:JSON.parseInt不是函数,javascript,node.js,json,Javascript,Node.js,Json,我对Node.js有问题。 我已经安装了nodemoden和express。 我的标题有个错误。谁能看看我的密码吗? 我有1个index.js文件和3个json文件。 另外,我有6个js文件,下面的1个是finds.js。其他5个文件是cheapmealsroute.js。largemealsroute.js等等 index.js const express = require('express'); const app = express(); const port = 3001; cons
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;
feeds.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
}
]
问题是JSON.parseInt不是一个函数 我不知道你从哪里得到的JSON有一个parseInt函数 首先解析JSON以将JSON用作JS对象(JSON.parse())
然后使用.parseInt()解析所需的整数将
JSON.parseInt
替换为JSON.parse
如果要将某些JSON转换为JavaScript对象,可以使用JSON.parse(myJson)
如果要将整数的字符串表示形式转换为数字类型,可以使用parseInt(myStr)
我想你可能只是混淆了两者。我想你的意思是
JSON.parse
,而不是parseInt
。正如错误所说,JSON.parseInt
不是函数。也许你的意思是JSON.parse
@AlexBroadwinparseInt
在那一餐上。JSON文件将产生NaN
非常感谢。没问题,祝你过得愉快:)非常感谢。非常感谢。