0

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 };
    });
  }

norbitrial
  • 14,716
  • 7
  • 32
  • 59
Valary o
  • 537
  • 10
  • 22
  • 1
    `model.findById()` is asynchronous. That means your function returns nothing and returns LONG before the callback is called. And, then your return statement is inside the callback so it just goes back into the callback, it doesn't return from your function. Go read the question yours was marked a dup of for how to return asynchronously retrieved values (using a callback or a promise). My recommnedation would be to use the promise interface on your database and return a promise. – jfriend00 Dec 27 '19 at 22:10
  • @jfriend00 Thanks for your help! I am reading the article and hope I'll solve my problem with that object – Valary o Dec 27 '19 at 22:15
  • @jfriend00, I just want to clarify, but can I return from that mongoose function a value different from the data it was fishing for in the db? Like I got the data, reorganized it in then section of a Promise and send back reorganized object wth the structure I need – Valary o Dec 27 '19 at 22:31
  • @jfriend00 I apologise for silly questions – Valary o Dec 27 '19 at 22:31
  • If you're using a callback to communicate back the result, you can pass whatever value you want to the callback (including your own custom object). If you're returning a promise, you can make the resolved value of the promise be whatever you want (including your own custom object). – jfriend00 Dec 27 '19 at 23:06
  • @jfriend00, I am playing around with that and I've figured out that I can reorganize my return in a Promise then section but I couldn't handle that in a callback with async/await – Valary o Dec 27 '19 at 23:11
  • @jfriend00, that's what I meant: https://stackoverflow.com/questions/59507000/how-can-i-reorganize-my-return-in-an-async-function – Valary o Dec 27 '19 at 23:38
  • @jfriend00, I'd like to learn all possible ways and good practices, maybe I omit something again because don't know much – Valary o Dec 27 '19 at 23:40

0 Answers0