I try to follow a project to learn mongoDB, Express...
But i'm blocked for the moment. Indeed, when i want to do a request on postman to update a user, i have this error (image):
events.js:352 throw er; // Unhandled 'error' event
Repository Git of this project: https://github.com/l-jonathan/projet-mern
I'm trying a lot of solutions find on the web but nothing work, if you have an idea, i take it !
Thanks !
You are mixing async-await
with promises in your updateUser
function.
Try to change it like this:
module.exports.updateUser = async (req, res) => {
if (!ObjectID.isValid(req.params.id))
return res.status(400).send('ID unknown ici : ' + req.params.id);
try {
const user = await UserModel.findOneAndUpdate(
{ _id: req.params.id },
{
$set: {
bio: req.body.bio,
},
},
{ new: true, upsert: true, setDefaultsOnInsert: true }
);
res.status(200).json(user);
} catch (err) {
return res.status(500).json({ message: err });
}
};
Also, in your server.js
file you shouldn't use the deprecated body-parser
library.
Change
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
To
app.use(express.json());
app.use(express.urlencoded({extended: true}));
And remove this line
const bodyParser = require('body-parser');
Finally, in your db.js
file you should add the useNewUrlParser
, useUnifiedTopology
and useFindAndModify
properties to the connect
method:
mongoose
.connect("mongodb+srv://" + process.env.DB_USER_PASS + "@cluster0.dui5l.mongodb.net/mern-project", {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.