Redirect to new Mongo Saved ID after Express POST? Redirect to new Mongo Saved ID after Express POST? express express

Redirect to new Mongo Saved ID after Express POST?


Of course you can.

The save() method can accept a callback so you can get the _id field of the saved document.

One way of do it would be:

newItem.save((err, itemSaved) => {    if(err) {        next(err);    }    const itemId = itemSaved._id;    res.redirect('/results/' + itemId);});

Hopefully, this will solve your problem.

P.S.

There's no need to use a colon on redirect's path. Beware with that, 'cause using it will give you a Cast Error.