我尝试使用React和MongoDB制作一个社交媒体应用程序项目,但是每当我尝试注册用户时,我都会从控制台收到一个POST错误。我尝试检查我的客户端和服务器端代码,但我仍然无法获得用户信息来发布和注册。
编辑谢谢你关于如何提出一个更好的问题的建议,这是我第二次尝试提出一个更清晰的问题
我在控制台中得到的主要错误是:
> POST http://localhost:3001/auth/register 500 (Internal Server Error)
> register @ Form.jsx:56 handleFormSubmit @ Form.jsx:89
> (anonymous) @ Formik.tsx:849 (anonymous) @ Formik.tsx:1200
> (anonymous) @ Formik.tsx:756 Promise.then (async)
> (anonymous) @ Formik.tsx:731 (anonymous) @ Formik.tsx:1200
> (anonymous) @ Formik.tsx:823 (anonymous) @ Formik.tsx:1200
> callCallback @ react-dom.development.js:4164
> invokeGuardedCallbackDev @ react-dom.development.js:4213
> invokeGuardedCallback @ react-dom.development.js:4277
> invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:4291
> executeDispatch @ react-dom.development.js:9041
> processDispatchQueueItemsInOrder @ react-dom.development.js:9073
> processDispatchQueue @ react-dom.development.js:9086
> dispatchEventsForPlugins @ react-dom.development.js:9097
> (anonymous) @ react-dom.development.js:9288
> batchedUpdates$1 @ react-dom.development.js:26140
> batchedUpdates @ react-dom.development.js:3991
> dispatchEventForPluginEventSystem @ react-dom.development.js:9287
> dispatchEventWithEnableCapturePhaseSelectiveHydrationWithoutDiscreteEventReplay @ react-dom.development.js:6465
> dispatchEvent @ react-dom.development.js:6457 dispatchDiscreteEvent
通过浏览器控制台进一步检查错误时,它将我指向Form.jsx文件中的以下代码行:
const savedUserResponse = await fetch("http://localhost:3001/auth/register", {
method: "POST",
body: formData,
});
下面是register函数的完整代码块:
const register = async (values, onSubmitProps) => {
//send form info with an image
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");
}
};
在我的服务器端代码中,这是处理auth的index.js:
// ROUTES WITH FILES
app.post("/auth/register", upload.single("picture"), register);
app.post("/posts", verifyToken, upload.single("picture"), createPost);
//ROUTES
app.use("/auth", authRoutes);
app.use("/users", userRoutes);
app.use("/posts", postRoutes);
// MONGOOSE
const PORT = process.env.PORT || 6001;
mongoose.set("strictQuery", false);
mongoose
.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
app.listen(PORT, () => console.log(`Server Port: ${PORT}`));
// User.insertMany(users);
// Post.insertMany(posts);
})
.catch((error) => console.log(`${error} did not connect`));
从我的控制器文件夹中的auth.js注册函数:
export const register = async (req, res) => {
try {
const { firstName, lastName, email, password, picturePath, friends, location, occupation } = req.body;
const salt = await bcrypt.genSalt();
const passwordHash = await bcrypt.hash(password, salt);
const newUser = new User({
firstName,
lastName,
email,
password: passwordHash,
picturePath,
friends,
location,
occupation,
viewedProfile: Math.floor(Math.random() * 10000),
impressions: Math.floor(Math.random() * 10000),
});
const savedUser = await newUser.save();
res.status(201).json(savedUser);
} catch (err) {
res.status(500).json({ error: err.message });
}
};
我从服务器得到的响应:
"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'
我希望用户信息发布到我的数据库后,我点击注册,但我得到内部服务器错误500
1条答案
按热度按时间oewdyzsn1#
如@Yarin_007所述,您需要提供更多信息才能获得最佳答案。不过,我会尽量提供最少的信息来帮助您。
状态500仅仅意味着服务器上有异常或错误。现在,警告已经提供了错误在哪里,为什么你会得到那个错误以及错误是什么。
其中:您可以在submitForm()中找到错误。
事件:这是一个语法错误,简单地说,这意味着你已经偏离了编程语言的字符序列或标记的语法。
原因:我们这样说是因为“〈”、“〈!DOCTYPE..."不是有效的JSON。
这是基于您提供的信息的错误说明。请获取有关错误的详细信息,以便我们提供帮助。
快乐编码!!!