If you see the code below, you'll notice that for GET request, 'add' page does not get any value passed, whereas for POST request, 'add' page gets 'msg' value as a result of posting.
router.get('/add', (req, res) => {
res.render('add', {})
})
router.post('/add', (req, res) => {
// receive a form
// save it to the database
// Promise is returned
.then(gig => res.render('add', {
msg: 'Your application is successful.'
})
.catch((err) => console.log(err));
}
})
In add.ejs file, I am not allowed to write <%= msg %> because it will cause an error for GET requests. Is there any other way than passing a { msg: null } to the GET router?
router.get('/add', (req, res) => {
res.render('add', { msg: null })
})
I'm asking this because I think passing null for cases like this is very repetitive.
CodePudding user response:
the alternative would be adding a control in the file you want to render
<% var msg = msg ?? null %>
this way if the variable msg isn't passed the value is null and the field will be empty