Reputation: 357
I'm trying to save a user to my database with the following code:
userRoutes.post('/signup', (req, res) => {
if (req.session.user) req.session.destroy();
const user = new User(req.body);
user.save()
.then(savedUser => {
req.session.user = savedUser._id;
res.status(200).json({
success: true,
data: {
firstName: savedUser.firstName,
lastName: savedUser.lastName,
username: savedUser.username,
email: savedUser.email,
dateCreated: savedUser.dataCreated,
_id: savedUser._id
},
});
})
.catch(error => {
res.status(500).json({
success: false,
error
});
});
});
But I still get this error: UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: cb is not a function
I'm using mongoose to save the document.
Upvotes: 0
Views: 384
Reputation: 29112
I've never used connect-mongo
and I don't have Mongo installed to give it a try myself but I did take a quick look through the source code to see if I could explain the problem you're having.
It seems the latest version (2.0.0) was released 3 weeks ago. I believe this change featured in that release:
https://github.com/jdesboeufs/connect-mongo/commit/a8b0fd49368cfc1ea634d4245105e706d2dd98c9
This changed how it invokes callbacks.
The handling of destroy
is here:
This then calls withCallback
, which is here:
I could be completely wrong but that seems to fit your symptoms perfectly. There's a function called cb
being invoked and it isn't checking whether it's undefined first.
A simple test would be to pass a callback function to destroy
. This shouldn't be required, I believe it's a bug in connect-mongo
, but it should help to confirm that this really is the source of the error you're seeing. You could further diagnose by attaching a debugger, such as the Chrome dev tools, to your Node and stepping through until you hit the error.
If this does turn out to be the problem then I suggest filing a bug report with connect-mongo
. From a quick look through the source code for some of the other session stores it does look like it's standard practice to check whether the callback is undefined before trying to call it.
I would add that I don't think destroy
is the method you should be using here anyway. I suspect you want regenerate
. However I believe this is a separate problem and not what's causing your current error.
I'd also note that while I stated it isn't required to pass a callback to destroy
, regenerate
, etc., it would still be a good idea as you should really wait for that to complete (or possibly fail) before you continue.
Upvotes: 1