Node.js
Using bcrypt inside async
seoca
2020. 9. 25. 23:18
Example code
app.post("/register", (req, res) => {
User.findOne({username: req.body.username}, async(err, doc) => {
if(err) throw err;
if(doc) res.send('User already exist');
if(!doc){
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const newUser = new User({
username: req.body.username,
password: hashedPassword,
});
await newUser.save();
res.send('User Created')
}
})
})
bcrypt 가 promise를 return하기에 async/await을 붙여야 error가 나지 않는다.
난 await을 빼먹어서 계속 에러가 났던 것...
Reference
stackoverflow.com/questions/48799894/trying-to-hash-a-password-using-bcrypt-inside-an-async-function
Trying to hash a password using bcrypt inside an async function
Following on from this question. I feel like I'm almost there, but my incomplete understanding of async is preventing me from solving this. I'm basically trying to just hash a password using bcryp...
stackoverflow.com
medium.com/dev-bits/writing-neat-asynchronous-node-js-code-with-promises-32ed3a4fd098
Writing neat asynchronous Node JS code with Promises
Write beautiful code with JavaScript Promises
medium.com