Using mongoose promises with async/await

前端 未结 5 430
谎友^
谎友^ 2020-12-13 13:19

I\'m trying to get the hang of using Mongoose promises with the async/await functionality of Node.js. When my function printEmployees is called I want to save t

相关标签:
5条回答
  • 2020-12-13 13:57

    You are not returning a Promise from orderEmployees.

    printEmployees: async(company) => {
      var employees = await self.orderEmployees(company);
      // SECOND CONSOLE.LOG
      console.log(employees);
    },
    
    orderEmployees: (companyID) => {
      return User.find({company:companyID})
     .exec()
     .then((employees) => {
       // FIRST CONSOLE.LOG
       console.log(employees);
       return employees;
     })
     .catch((err) => {
       return 'error occured';
     });
    },
    
    0 讨论(0)
  • 2020-12-13 14:02

    You need to return a Promise from orderEmployees

    orderEmployees: companyId => User.find({ companyId }).exec()
    

    If you want to do some error handling or pre-processing before you return then you can keep your code as is but just remember to return the result (promises are chainable).

    0 讨论(0)
  • 2020-12-13 14:07

    In order to make orderEmployees behave like async functions, you have to return the resulting promise. There are two rules to follow when using promises without async/await keywords:

    1. A function is asynchronous if it returns a Promise
    2. If you have a promise (for example returned by an async function) you must either call .then on it or return it.

    When you are using async/await then you must await on promises you obtain.

    This said you will notice that you do not return the promise generated inside orderEmployees. Easy to fix, but its also easy to rewrite that function to async too.

    orderEmployees: (companyID) => {
      return User.find({company:companyID}) // Notice the return here
      .exec()
      .then((employees) => {
        // FIRST CONSOLE.LOG
        console.log(employees);
        return employees;
      })
      .catch((err) => {
        return 'error occured';
      });
    },
    

    or

    orderEmployees: async(companyID) => {
      try {
        const employees = await User.find({company:companyID}).exec();
        console.log(employees);
        return employees;
      } catch (err) {
        return 'error occured';
      }
    },
    

    PS: the error handling is somewhat flawed here. We usually do not handle errors by returning an error string from a function. It is better to have the error propagate in this case, and handle it from some top-level, UI code.

    0 讨论(0)
  • 2020-12-13 14:09

    You need to return your Promise.

    • Currently, you are awaiting on a function that returns undefined.
    • await only actually "waits" for the value if it's used with a Promise.
    • Always keep in mind that you can only await Promises or async functions, which implicitly return a Promise1.

    orderEmployees: (companyID) => {
      return User.find({ company:companyID }).exec()
    }
    

    Also really important, you should throw instead of return in your .catch handler. Returning from within a .catch handler will cause the promise chain to trigger it's .then instead of it's .catch thus breaking the error handling chain.

    Better yet, don't include .catch at all and let the the actual error bubble up the promise chain, instead of overriding it with your own non-descriptive 'error occured' message.

    Error conditions should throw the error, not return it.


    1 You can also await non-Promises, but only for values that are evaluated synchronously.

    0 讨论(0)
  • 2020-12-13 14:14

    if you're going to use async/await then it works like this.

    • await in front of the function that returns a promise.
    • async in front of the wrapping function.
    • wrap the function body inside try/catch block.

    Please have a look on this function, it is a middleware before i execute a specific route in express.

    const validateUserInDB = async (req, res, next) => {
        try {
            const user = await UserModel.findById(req.user._id);
            if (!user) return res.status(401).json({ message: "Unauthorized." });
            req.user = user;
            return next();
        } catch (error) {
            return res.status(500).json({ message: "Internal server error." })
        }
    }
    
    • The code after await is waiting the promise to be resolved.
    • Catch block catches any error happened inside the try block even if the error that is triggered by catch method comes from awaiting promise.
    0 讨论(0)
提交回复
热议问题