I've got a base class which is inherited by user class.
Base class has functions like: findById
, findByEmail
, findAll
.
In my user class I am checking for a user if it exists before doing any further logic.
But I cannot make the object:
return { message: `User with id: ${id}`, success: true, user: user };
return to my user class. I think it would be much more comfortable to have the object returned than just null.
I'd like to hear experienced people because I am new to all this stuff. This object would be useful when I need to inform the client what happened and\or why he received or didn't receive expected information. How is this relationship usually implemented?
findById(id) {
this.model.findById(id, (err, user) => {
if (err) {
logger.error(err.message);
return { message: err.message, success: false, user: null };
}
if (!user) {
logger.warn(`Coundn't find user with id: ${id}`);
return { message: `Coundn't find user with id: ${id}`, success: false, user: null };
}
return { message: `User with id: ${id}`, success: true, user: user };
});
}