从我自己的API将数据发送给MongoDB的错误
实际上,我正在开发一个弹奏应用程序,该应用程序将数据发送到我自己在节点js中创建的API,然后当我测试API时,数据将数据发送到MongoDB群集,因此它可以正常工作,并且数据转到MongoDB,但是当我尝试发送时,我的Flutter应用程序中的数据给出以下错误:
(node:4856) UnhandledPromiseRejectionWarning: ValidationError: User validation failed: name: Path `name` is required., email: Path `email` is required., password: Path `password` is required.
我认为所需的数据未接收到MongoDB,因此我创建了一个简单的JavaScript应用程序,将该数据发送到API,但是我收到了相同的错误,我认为这是我的API的错误,但是当我从邮递员或雷霆客户发送数据时,仍然可以。这是我创建的API的代码:
const DB = "mydburl"
app.use(express.json());
app.post('/api/signup', async (req, res) => {
const {name, email, password} = req.body;
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ msg: "User with same email already already exists" })
}
const hashedPassword = await bcryptjs.hash(password, 8);
let user = new User({
email,
password : hashePassword,
name,
});
res.json(user)
user = await user.save();
console.log(user);
});
mongoose.connect(DB).then(() => {
console.log("connection succesful")
})
.catch((e) => {
console.log(e);
})
app.listen(PORT, "0.0.0.0", () => {
console.log(`connected at port ${PORT}`)
});
Actually, I was developing a flutter application that sends the data to my own API created in node js and then the data goes to MongoDB cluster, when I tested the API so it was working and the data goes to MongoDB but when I tried to send the data from my flutter app so it is giving the following error:
(node:4856) UnhandledPromiseRejectionWarning: ValidationError: User validation failed: name: Path `name` is required., email: Path `email` is required., password: Path `password` is required.
I think that the required data is not receiving to MongoDB so I created a simple javascript app that sends that data to the API, but I WAS receiving the same error, I think that this is the error from my API, but It is still ok when I send the data from the postman, or thunder-client. This is the code of API I have created:
const DB = "mydburl"
app.use(express.json());
app.post('/api/signup', async (req, res) => {
const {name, email, password} = req.body;
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ msg: "User with same email already already exists" })
}
const hashedPassword = await bcryptjs.hash(password, 8);
let user = new User({
email,
password : hashePassword,
name,
});
res.json(user)
user = await user.save();
console.log(user);
});
mongoose.connect(DB).then(() => {
console.log("connection succesful")
})
.catch((e) => {
console.log(e);
})
app.listen(PORT, "0.0.0.0", () => {
console.log(`connected at port ${PORT}`)
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)