Node.JS Promises and Logging in [closed]
Clash 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!
node.js promise
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
add a comment |Â
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!
node.js promise
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
add a comment |Â
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!
node.js promise
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!
node.js promise
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
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
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Apr 17 at 15:02
sineemore
1,235219
1,235219
add a comment |Â
add a comment |Â