I’m following a tutorial, trying to learn MERN. I’ve created function that send the data to database, but I can’t see this data in the data base. Code bellow
<code>export const signup = async (req, res) => {
try {
const { fullName, username, password, confirmPassword, gender } = req.body;
if (password !== confirmPassword) {
return res.status(400).json({ error: "Passwords don't match" });
}
const user = await User.findOne({ username });
if (user) {
return res.status(400).json({ error: "Username already exists" });
}
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
const boyProfilePic = `https://avatar.iran.liara.run/public/boy?username=${username}`;
const girlProfilePic = `https://avatar.iran.liara.run/public/girl?username=${username}`;
const newUser = new User({
fullName,
username,
password: hashedPassword,
gender,
profilePic: gender === "male" ? boyProfilePic : girlProfilePic,
});
if (newUser) {
generateTokenAndSendCookie(newUser._id, res);
await newUser.save();
res.status(201).json({
_id: newUser._id,
fullName: newUser.fullName,
username: newUser.username,
profilePic: newUser.profilePic,
});
} else {
res.status(400).json({ error: "Invalid user data" });
}
} catch (error) {
console.log("Error in signup controller", error.message);
res.status(500).json({ error: "Internal Server Error" });
}
}
</code>
<code>export const signup = async (req, res) => {
try {
const { fullName, username, password, confirmPassword, gender } = req.body;
if (password !== confirmPassword) {
return res.status(400).json({ error: "Passwords don't match" });
}
const user = await User.findOne({ username });
if (user) {
return res.status(400).json({ error: "Username already exists" });
}
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
const boyProfilePic = `https://avatar.iran.liara.run/public/boy?username=${username}`;
const girlProfilePic = `https://avatar.iran.liara.run/public/girl?username=${username}`;
const newUser = new User({
fullName,
username,
password: hashedPassword,
gender,
profilePic: gender === "male" ? boyProfilePic : girlProfilePic,
});
if (newUser) {
generateTokenAndSendCookie(newUser._id, res);
await newUser.save();
res.status(201).json({
_id: newUser._id,
fullName: newUser.fullName,
username: newUser.username,
profilePic: newUser.profilePic,
});
} else {
res.status(400).json({ error: "Invalid user data" });
}
} catch (error) {
console.log("Error in signup controller", error.message);
res.status(500).json({ error: "Internal Server Error" });
}
}
</code>
export const signup = async (req, res) => {
try {
const { fullName, username, password, confirmPassword, gender } = req.body;
if (password !== confirmPassword) {
return res.status(400).json({ error: "Passwords don't match" });
}
const user = await User.findOne({ username });
if (user) {
return res.status(400).json({ error: "Username already exists" });
}
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
const boyProfilePic = `https://avatar.iran.liara.run/public/boy?username=${username}`;
const girlProfilePic = `https://avatar.iran.liara.run/public/girl?username=${username}`;
const newUser = new User({
fullName,
username,
password: hashedPassword,
gender,
profilePic: gender === "male" ? boyProfilePic : girlProfilePic,
});
if (newUser) {
generateTokenAndSendCookie(newUser._id, res);
await newUser.save();
res.status(201).json({
_id: newUser._id,
fullName: newUser.fullName,
username: newUser.username,
profilePic: newUser.profilePic,
});
} else {
res.status(400).json({ error: "Invalid user data" });
}
} catch (error) {
console.log("Error in signup controller", error.message);
res.status(500).json({ error: "Internal Server Error" });
}
}
When I try to go to the /api/auth/signup I receive this.
cannot GET /api/auth/signup
Maybe the problem is that I’m using a POST instead of GET request?
<code>import express from "express";
import { login, logout, signup } from "../contollers/auth.controller.js";
const router = express.Router();
router.post("/signup", signup);
router.post("/login", login);
router.post("/logout", logout);
export default router;
</code>
<code>import express from "express";
import { login, logout, signup } from "../contollers/auth.controller.js";
const router = express.Router();
router.post("/signup", signup);
router.post("/login", login);
router.post("/logout", logout);
export default router;
</code>
import express from "express";
import { login, logout, signup } from "../contollers/auth.controller.js";
const router = express.Router();
router.post("/signup", signup);
router.post("/login", login);
router.post("/logout", logout);
export default router;
But even when I try to send the data, I receive this error
Error in signup controller Illegal arguments: undefined, string
Generally I don’t know what to do here