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}`)
});
app.post('/api/signup', async (req, res) => {
const {name, email, password} = req.body;
console.log(`name=${name} , email = ${email} , password=${password}`);
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ msg: "User with same email already already exists" })
}
const hashedPassword = bcryptjs.hashSync(password, 8);
let user = new User({
email:email,
password : hashePassword,
name:name
});
user.save().then((userSaved)=>{
return res.status(200).json(user);
}).catch((e)=>{
return res.status(500).json({status:"error",error:e})
});
});