Here's a snippet of my code where I utilize the app.all
method. In this scenario, I am invoking the fetchBuildings
function based on the building ID or hash provided in the URL. Subsequently, I am assigning values to the title
, description
, and image
properties based on the response received:
app.all('/:id', function (req, res) {
const hash = req.params.id
const obj = {}
if (hash === 'undefined') {
obj.title = 'iStaging LiveTour'
obj.description = ''
obj.image = 'https://raw.githubusercontent.com/alexcheninfo/vue-tmux-example/master/app/istaging.jpg'
return
}
fetchBuildingsByHash(hash).then(({title, description, image, isBasicPlan}) => {
if (isBasicPlan) {
obj.title = 'iStaging LiveTour'
obj.description = ''
obj.image = 'https://raw.githubusercontent.com/alexcheninfo/vue-tmux-example/master/app/istaging.jpg'
} else {
obj.title = title || 'iStaging LiveTour'
obj.description = description || ''
obj.image = image || 'https://raw.githubusercontent.com/alexcheninfo/vue-tmux-example/master/app/istaging.jpg'
}
res.render('index.ejs', obj)
}).catch((err) => {
const obj = {
title: 'notFound'
}
res.render('404.ejs', obj)
})
});
In certain cases, the value of hash
may be 'undefined'
, prompting me to halt the execution of the code when that condition is met.
Currently, I am using the return
statement for this purpose. However, I am curious if there is a more conventional or 'proper' approach to handling this situation. Is there an alternative method that should be considered?