[英]SyntaxError: Unexpected token t in JSON.parse
I want to use bodyParser
on express.我想在 express 上使用
bodyParser
。 My posts.js
file has我的
posts.js
文件有
const express = require('express');
const router = express.Router();
const Post = require('../models/Post');
router.post('/', (req, res) => {
console.log(req.body);
});
module.exports = router;
On app.js
file, the code is like在
app.js
文件上,代码就像
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const postsRoutes = require('./routes/posts');
// const usersRoutes = require('./routes/users');
app.use('/posts', postsRoutes);
// app.use('/users', usersRoutes);
Dependencies versions are依赖版本是
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.9.7",
"nodemon": "^2.0.3"
Server connected successfully but when I pass the data through postman on json mode, the terminal shows error like SyntaxError: Unexpected token t in JSON at position 3 at JSON.parse (<anonymous>)...
I used the following code on app.js
Server connected successfully but when I pass the data through postman on json mode, the terminal shows error like
SyntaxError: Unexpected token t in JSON at position 3 at JSON.parse (<anonymous>)...
I used the following code on app.js
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
because now express
has buiild-in body-person
function but output same here.因为现在
express
有内置的body-person
function 但 output 在这里相同。
What can I do to solve this problem?我能做些什么来解决这个问题? I need help.
我需要帮助。 Thanks in advance!
提前致谢!
In JSON keys must be strings, try the following in the postman payload:在 JSON 中的键必须是字符串,在 postman 有效负载中尝试以下操作:
{
"title": "Title here",
"Description": "Description here..."
}
Instead of this:而不是这个:
{
title: "Title here",
Description: "Description here..."
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.