I have been working on a social media app project using React and MongoDB. However, every time I try to register a user, I encounter a POST error in the console. I have reviewed both my client-side and server-side code, but I am still unable to successfully post and register user information.
**EDIT ** Thank you for the guidance on how to improve the question. Here is my revised attempt at making a clearer post:
The primary error appearing in the console is as follows:
> POST http://localhost:3001/auth/register 500 (Internal Server Error)
> register @ Form.jsx:56
> handleFormSubmit @ Form.jsx:89
Upon further investigation of the error in the browser console, it has led me to this line of code in my Form.jsx file:
const savedUserResponse = await fetch("http://localhost:3001/auth/register", {
method: "POST",
body: formData,
});
This snippet displays the complete function block for my register functionality:
const register = async (values, onSubmitProps) => {
const formData = new FormData();
for (let value in values) {
formData.append(value, values[value]);
}
formData.append("picturePath", values.picture.name);
const savedUserResponse = await fetch("http://localhost:3001/auth/register", {
method: "POST",
body: formData,
});
const savedUser = await savedUserResponse.json();
onSubmitProps.resetForm();
if (savedUser) {
setPageType("login");
}
};
In my server-side code, here is the index.js responsible for handling authentication:
// ROUTES WITH FILES
app.post("/auth/register", upload.single("picture"), register);
// more routes...
// ROUTES
app.use("/auth", authRoutes);
app.use("/users", userRoutes);
app.use("/posts", postRoutes);
// MONGOOSE
const PORT = process.env.PORT || 6001;
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => {
app.listen(PORT, () => console.log(`Server Port: ${PORT}`));
}).catch((error) => console.log(`${error} did not connect`));
Below is the register function from my auth.js located in the controllers folder:
export const register = async (req, res) => {
try {
// User registration logic...
} catch (err) {
res.status(500).json({ error: err.message });
}
};
The server response I receive reads:
"POST /auth/register HTTP/1.1" 500 248
Error: ENOENT: no such file or directory, open 'G:\WebDev\DesTracker\destracker-app\server\public,assets\p2.jpeg'
After clicking on the register button, I expect user details to be posted to the database, but instead, I am facing an internal server error 500.