'SyntaxError: Unexpected token l in JSON at position
Why do I get this error every time I use the api (POST)
SyntaxError: Unexpected token l in JSON at position 18 at JSON.parse (<anonymous>) at parse (/Users/.../Desktop/ecommerceapi/node_modules/body-parser/lib/types/json.js:89:19) at /Users/.../Desktop/ecommerceapi/node_modules/body-parser/lib/read.js:121:18 at invokeCallback (/Users/.../Desktop/ecommerceapi/node_modules/raw-body/index.js:224:16) at done (/Users/.../Desktop/ecommerceapi/node_modules/raw-body/index.js:213:7) at IncomingMessage.onEnd (/Users/.../Desktop/ecommerceapi/node_modules/raw-body/index.js:273:7) at IncomingMessage.emit (node:events:532:35) at endReadableNT (node:internal/streams/readable:1346:12) at processTicksAndRejections (node:internal/process/task_queues:83:21)
This is my entire code: (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;
I'm using Postman to use the API to POST this
{ "username": sultan "password": 12345qwer }
and this is 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!");
});
it should give me the users Info or "Wrong Password" but it gives the above error as a response!
Solution 1:[1]
double quotes and comma are missing { "username": "sultan", "password": "12345 qwer" }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | l4rnaud |
