Is it possible to add a custom response in Express JS, like add an attribute that not exists in the database?
My Controller:
createNewPersonalData: function (req, res) {
var name = req.body.name,
date_of_birth = req.body.date_of_birth;
var getAge = function () {
var today = new Date();
var dob = new Date(date_of_birth);
var count = today.getFullYear() - dob.getFullYear();
var m = today.getMonth() - dob.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
count = count - 1;
}
return count;
};
PersonalInfo.create({
name : name,
date_of_birth : date_of_birth,
age : getAge()
}).then(data => {
res.json({
'status': 'OK',
'messages': 'Personal Info Created',
'data': data
})
});
but the response is only all attribute from the database table. There's no attribute/field age.
{
"code": 200,
"status": true,
"data": {
"id": 1,
"name": "John",
"date_of_birth": "1995-01-28T17:00:00.000Z",
}
}
the response what I expected is like:
{
"code": 200,
"status": true,
"data": {
"id": 1,
"name": "John",
"date_of_birth": "1995-01-28T17:00:00.000Z",
"age": 24
}
}
How do I can add that age?
getAge()contains?curren_date - date_of_birthand convert into years