Im new to the world of Nunjucks / Express and Node.
I have a routes file which is porting in the value of an input from a form field. I need to check if the value contains the expression 'gov'.
So i created the following:
router.get('/test/output/', function (req, res) {
var emailAdress = req.session.data['accountEmail']
if ('gov' in emailAdress){
isCS = true
}
res.render('test/output.html',{
'email' : emailAdress,
.... and so on...
And this gives me the following error:
TypeError: Cannot use 'in' operator to search for 'gov' in 'email@email.com'
I understand that this is to do with objects and arrays - I just cannot find the solution - I appreciate its probably straightforward. Any help is much appreciated.
The in
operator only works on objects, not string primitives.
To search within a string, you can use includes
:
emailAdress.includes('gov')
An ES5 compatible version (before includes
existed):
emailAdress.indexOf('gov') !== -1
Or perhaps you can check if the string ends with 'gov'
:
emailAdress.endsWith('gov')