Node.JS Promises and Logging in [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
-2
down vote

favorite












I'm learning about Node.js and also Express.js, and attempting to create a forum like website in the process of such,



I currently want the user to be able to register an account, and then log into it which is handled via MongoDB using Mongoose



async function FindUser(username)

return new Promise((res, rej) =>
UserModel.findOne(username: username, (err, user)=>
if (err)
rej(error);
if (!user)
rej("User not found..");
res(user);
)
)


module.exports.loginUser = async function(username, password)

return new Promise((res, rej) =>
let User = await FindUser(username);
// Other logic below..


// We've logged in!
res()
);



After the resolve, we simply go back to a router where it handles the promise.



I'm not sure if this is how I should be using promises, and if anyone can provide insight on how to handle these async tasks, it would be much appreciated!







share|improve this question











closed as off-topic by 200_success, Sam Onela, Mast, hjpotter92, alecxe Apr 17 at 21:36


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Sam Onela, Mast, hjpotter92, alecxe
If this question can be reworded to fit the rules in the help center, please edit the question.


















    up vote
    -2
    down vote

    favorite












    I'm learning about Node.js and also Express.js, and attempting to create a forum like website in the process of such,



    I currently want the user to be able to register an account, and then log into it which is handled via MongoDB using Mongoose



    async function FindUser(username)

    return new Promise((res, rej) =>
    UserModel.findOne(username: username, (err, user)=>
    if (err)
    rej(error);
    if (!user)
    rej("User not found..");
    res(user);
    )
    )


    module.exports.loginUser = async function(username, password)

    return new Promise((res, rej) =>
    let User = await FindUser(username);
    // Other logic below..


    // We've logged in!
    res()
    );



    After the resolve, we simply go back to a router where it handles the promise.



    I'm not sure if this is how I should be using promises, and if anyone can provide insight on how to handle these async tasks, it would be much appreciated!







    share|improve this question











    closed as off-topic by 200_success, Sam Onela, Mast, hjpotter92, alecxe Apr 17 at 21:36


    This question appears to be off-topic. The users who voted to close gave this specific reason:


    • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Sam Onela, Mast, hjpotter92, alecxe
    If this question can be reworded to fit the rules in the help center, please edit the question.














      up vote
      -2
      down vote

      favorite









      up vote
      -2
      down vote

      favorite











      I'm learning about Node.js and also Express.js, and attempting to create a forum like website in the process of such,



      I currently want the user to be able to register an account, and then log into it which is handled via MongoDB using Mongoose



      async function FindUser(username)

      return new Promise((res, rej) =>
      UserModel.findOne(username: username, (err, user)=>
      if (err)
      rej(error);
      if (!user)
      rej("User not found..");
      res(user);
      )
      )


      module.exports.loginUser = async function(username, password)

      return new Promise((res, rej) =>
      let User = await FindUser(username);
      // Other logic below..


      // We've logged in!
      res()
      );



      After the resolve, we simply go back to a router where it handles the promise.



      I'm not sure if this is how I should be using promises, and if anyone can provide insight on how to handle these async tasks, it would be much appreciated!







      share|improve this question











      I'm learning about Node.js and also Express.js, and attempting to create a forum like website in the process of such,



      I currently want the user to be able to register an account, and then log into it which is handled via MongoDB using Mongoose



      async function FindUser(username)

      return new Promise((res, rej) =>
      UserModel.findOne(username: username, (err, user)=>
      if (err)
      rej(error);
      if (!user)
      rej("User not found..");
      res(user);
      )
      )


      module.exports.loginUser = async function(username, password)

      return new Promise((res, rej) =>
      let User = await FindUser(username);
      // Other logic below..


      // We've logged in!
      res()
      );



      After the resolve, we simply go back to a router where it handles the promise.



      I'm not sure if this is how I should be using promises, and if anyone can provide insight on how to handle these async tasks, it would be much appreciated!









      share|improve this question










      share|improve this question




      share|improve this question









      asked Apr 17 at 14:00









      Lewis Wood

      11




      11




      closed as off-topic by 200_success, Sam Onela, Mast, hjpotter92, alecxe Apr 17 at 21:36


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Sam Onela, Mast, hjpotter92, alecxe
      If this question can be reworded to fit the rules in the help center, please edit the question.




      closed as off-topic by 200_success, Sam Onela, Mast, hjpotter92, alecxe Apr 17 at 21:36


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Sam Onela, Mast, hjpotter92, alecxe
      If this question can be reworded to fit the rules in the help center, please edit the question.




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I have checked mongoose documentation. Seems like it supports promises already. See this.



          So, there is no need to call new Promise directly:



          async function FindUser(username) 
          return await UserModel.findOne(username: username).exec();


          module.exports.loginUser = async function(username, password)
          user = await FindUser(username);
          /* Doing user things here */
          return user;



          Note: you hardly ever need to create promise yourself, most of the time this is considered antipattern.






          share|improve this answer




























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            I have checked mongoose documentation. Seems like it supports promises already. See this.



            So, there is no need to call new Promise directly:



            async function FindUser(username) 
            return await UserModel.findOne(username: username).exec();


            module.exports.loginUser = async function(username, password)
            user = await FindUser(username);
            /* Doing user things here */
            return user;



            Note: you hardly ever need to create promise yourself, most of the time this is considered antipattern.






            share|improve this answer

























              up vote
              1
              down vote



              accepted










              I have checked mongoose documentation. Seems like it supports promises already. See this.



              So, there is no need to call new Promise directly:



              async function FindUser(username) 
              return await UserModel.findOne(username: username).exec();


              module.exports.loginUser = async function(username, password)
              user = await FindUser(username);
              /* Doing user things here */
              return user;



              Note: you hardly ever need to create promise yourself, most of the time this is considered antipattern.






              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                I have checked mongoose documentation. Seems like it supports promises already. See this.



                So, there is no need to call new Promise directly:



                async function FindUser(username) 
                return await UserModel.findOne(username: username).exec();


                module.exports.loginUser = async function(username, password)
                user = await FindUser(username);
                /* Doing user things here */
                return user;



                Note: you hardly ever need to create promise yourself, most of the time this is considered antipattern.






                share|improve this answer













                I have checked mongoose documentation. Seems like it supports promises already. See this.



                So, there is no need to call new Promise directly:



                async function FindUser(username) 
                return await UserModel.findOne(username: username).exec();


                module.exports.loginUser = async function(username, password)
                user = await FindUser(username);
                /* Doing user things here */
                return user;



                Note: you hardly ever need to create promise yourself, most of the time this is considered antipattern.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Apr 17 at 15:02









                sineemore

                1,235219




                1,235219












                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation