SyntaxError: Unexpected token l in JSON at position
SyntaxError: Unexpected token l in JSON at position
为什么每次使用 api (POST)
时都会出现此错误
SyntaxError:JSON 中位置 18 的意外标记 l
在 JSON.parse(<匿名>)
在解析 (/Users/.../Desktop/ecommerceapi/node_modules/body-parser/lib/types/json.js:89:19)
在 /Users/.../Desktop/ecommerceapi/node_modules/body-parser/lib/read.js:121:18
在 invokeCallback (/Users/.../Desktop/ecommerceapi/node_modules/raw-body/index.js:224:16)
完成 (/Users/.../Desktop/ecommerceapi/node_modules/raw-body/index.js:213:7)
在 IncomingMessage.onEnd (/Users/.../Desktop/ecommerceapi/node_modules/raw-body/index.js:273:7)
在 IncomingMessage.emit(节点:事件:532:35)
在 endReadableNT(节点:internal/streams/readable:1346:12)
在 processTicksAndRejections(节点:internal/process/task_queues:83:21)
这是我的全部代码:(auth.js)
const router = require("express").Router();
const User = require("../models/User");
const CryptoJS = require("crypto-js");
const jwt = require("jsonwebtoken");
//REGISTER
router.post("/register", async (req, res) => {
const newUser = new User({
username: req.body.username,
email: req.body.email,
password: CryptoJS.AES.encrypt(
req.body.password,
process.env.PASS_SEC
).toString(),
});
try {
const savedUser = await newUser.save();
res.status(201).json(savedUser);
} catch (err) {
res.status(500).json(err);
}
});
//LOGIN
router.post('/login', async (req, res) => {
try{
const user = await User.findOne(
{
userName: req.body.user_name
}
);
!user && res.status(401).json("Wrong User Name");
const hashedPassword = CryptoJS.AES.decrypt(
user.password,
process.env.PASS_SEC
);
const originalPassword = hashedPassword.toString(CryptoJS.enc.Utf8);
const inputPassword = req.body.password;
originalPassword != inputPassword &&
res.status(401).json("Wrong Password");
const accessToken = jwt.sign(
{
id: user._id,
isAdmin: user.isAdmin,
},
process.env.JWT_SEC,
{expiresIn:"3d"}
);
const { password, ...others } = user._doc;
res.status(200).json({...others, accessToken});
}catch(err){
res.status(500).json(err);
}
});
module.exports = router;
我正在使用 Postman 来使用 API 到 POST 这个
{ "username": sultan "password": 12345qwer }
这是 index.js:
const express = require("express");
const app = express();
mongoose = require("mongoose")
const dotenv = require("dotenv");
const userRoute = require("./routes/user");
const authRoute = require("./routes/auth");
dotenv.config();
mongoose
.connect(process.env.MONGO_URL)
.then(() => console.log("DB Connection Successfull!"))
.catch((err) => {
console.log(err);
});
app.use(express.json());
app.use("/api/auth", authRoute);
app.use("/api/users", userRoute);
app.listen(process.env.PORT || 50, () => {
console.log("Backend server is running!");
});
它应该给我用户信息或“密码错误”,但它给出了上述错误作为响应!
缺少双引号和逗号
{
“用户名”:“苏丹”,
“密码”:“12345 qwer”
}
为什么每次使用 api (POST)
时都会出现此错误SyntaxError:JSON 中位置 18 的意外标记 l 在 JSON.parse(<匿名>) 在解析 (/Users/.../Desktop/ecommerceapi/node_modules/body-parser/lib/types/json.js:89:19) 在 /Users/.../Desktop/ecommerceapi/node_modules/body-parser/lib/read.js:121:18 在 invokeCallback (/Users/.../Desktop/ecommerceapi/node_modules/raw-body/index.js:224:16) 完成 (/Users/.../Desktop/ecommerceapi/node_modules/raw-body/index.js:213:7) 在 IncomingMessage.onEnd (/Users/.../Desktop/ecommerceapi/node_modules/raw-body/index.js:273:7) 在 IncomingMessage.emit(节点:事件:532:35) 在 endReadableNT(节点:internal/streams/readable:1346:12) 在 processTicksAndRejections(节点:internal/process/task_queues:83:21)
这是我的全部代码:(auth.js)
const router = require("express").Router();
const User = require("../models/User");
const CryptoJS = require("crypto-js");
const jwt = require("jsonwebtoken");
//REGISTER
router.post("/register", async (req, res) => {
const newUser = new User({
username: req.body.username,
email: req.body.email,
password: CryptoJS.AES.encrypt(
req.body.password,
process.env.PASS_SEC
).toString(),
});
try {
const savedUser = await newUser.save();
res.status(201).json(savedUser);
} catch (err) {
res.status(500).json(err);
}
});
//LOGIN
router.post('/login', async (req, res) => {
try{
const user = await User.findOne(
{
userName: req.body.user_name
}
);
!user && res.status(401).json("Wrong User Name");
const hashedPassword = CryptoJS.AES.decrypt(
user.password,
process.env.PASS_SEC
);
const originalPassword = hashedPassword.toString(CryptoJS.enc.Utf8);
const inputPassword = req.body.password;
originalPassword != inputPassword &&
res.status(401).json("Wrong Password");
const accessToken = jwt.sign(
{
id: user._id,
isAdmin: user.isAdmin,
},
process.env.JWT_SEC,
{expiresIn:"3d"}
);
const { password, ...others } = user._doc;
res.status(200).json({...others, accessToken});
}catch(err){
res.status(500).json(err);
}
});
module.exports = router;
我正在使用 Postman 来使用 API 到 POST 这个
{ "username": sultan "password": 12345qwer }
这是 index.js:
const express = require("express");
const app = express();
mongoose = require("mongoose")
const dotenv = require("dotenv");
const userRoute = require("./routes/user");
const authRoute = require("./routes/auth");
dotenv.config();
mongoose
.connect(process.env.MONGO_URL)
.then(() => console.log("DB Connection Successfull!"))
.catch((err) => {
console.log(err);
});
app.use(express.json());
app.use("/api/auth", authRoute);
app.use("/api/users", userRoute);
app.listen(process.env.PORT || 50, () => {
console.log("Backend server is running!");
});
它应该给我用户信息或“密码错误”,但它给出了上述错误作为响应!
缺少双引号和逗号 { “用户名”:“苏丹”, “密码”:“12345 qwer” }