Reputation: 33
I have a slug
param in my API url /photo/:slug/
. After it has completed, it redirects to another page which has the slug in its url. How do I insert the slug into the redirect url?
Here is my route where I am uploading a file to the server:
exports.postEntriesCollection = (req, res) => {
Campaign.findOne({slug:req.params.slug}, function(err, campaign) {
let filename = ''
if(!isEmpty(req.files)) {
let file = req.files.file;
filename = file.name;
let uploadDir = './public/PhotoContestUploads/uploads/';
console.log(filename);
file.mv(uploadDir+filename, (err) => {
if (err)
throw err;
});
}
const photo = new PhotoEntries({
Email: req.body.email,
Name: req.body.name,
Description: req.body.description,
Phonenumber: req.body.Phone,
CampaignId: campaign._id,
done: false,
Photo: `uploads/${filename}`
});
photo.save().then(post => {
console.log(photo);
req.flash('success', { msg: 'Uploaded successfully' });
res.redirect('/photoapp/**:slug**/success');
});
});
}
How do I do this?
Upvotes: 1
Views: 759
Reputation: 63059
Since slug
is just a string that you want inserted into another string, do:
res.redirect('/photoapp/' + req.params.slug + '/success');
or this if you prefer:
res.redirect(`/photoapp/${req.params.slug}/success`);
Upvotes: 1