我想把这个回调函数改成async/await,但是不知道怎么改
I want to change this callback function to async/await, but I don't know how to change this
User.findOne({ email: email })
.then((savedUser) => {
if (savedUser) {
return res
.status(422)
.json({ error: "user already exists with that email" });
}
bcrypt.hash(password, 12).then((hashedpassword) => {
const user = new User({
email,
password: hashedpassword,
name,
pic,
});
user
.save()
.then((user) => {
res.json({ message: "saved successfully" });
})
.catch((err) => {
console.log(err);
});
});
})
.catch((err) => {
console.log(err);
});
});
我正在研究 async/await 将一些回调函数更改为 async/await。此代码有两个捕获错误,但我不知道如何将此代码更改为 async/await.
const saveUser = async (email,password,name,pic)=>{
try{
const user = new User({
email,
password: await bcrypt.hash(password, 12),
name,
pic,
})
const savedUser = await User.save()
return savedUser
}catch(error){
throw new Error(error)
}
}
const findUser = async (email)=>{
try{
const user = await User.findOne({ email: email })
return user
}catch(error){
throw new Error(error)
}
}
要记住的关键是你可以 await
on Promise
(async
关键字就像一个装饰器,它强制函数 return a Promise
).
以下方法 return 保证您可以使用 await
User.findOne(...)
bcrypt.hash(...)
user.save()
重构后的代码将如下所示:
async function functionName(...) {
try {
const savedUser = await User.findOne({ email: email });
}
catch (e) {
//...
}
if (savedUser) {
return res
.status(422)
.json({ error: "user already exists with that email" });
}
const hashedpassword = await bcrypt.hash(password, 12)
const user = new User({
email,
password: hashedpassword,
name,
pic,
});
try {
await user.save();
}
catch (e) {
return res
.status(422)
.json({ error: "...ERROR..." });
}
res.json({ message: "saved successfully" });
}
User.findOne({ email: email })
.then((savedUser) => {
if (savedUser) {
return res
.status(422)
.json({ error: "user already exists with that email" });
}
bcrypt.hash(password, 12).then((hashedpassword) => {
const user = new User({
email,
password: hashedpassword,
name,
pic,
});
user
.save()
.then((user) => {
res.json({ message: "saved successfully" });
})
.catch((err) => {
console.log(err);
});
});
})
.catch((err) => {
console.log(err);
});
});
我正在研究 async/await 将一些回调函数更改为 async/await。此代码有两个捕获错误,但我不知道如何将此代码更改为 async/await.
const saveUser = async (email,password,name,pic)=>{
try{
const user = new User({
email,
password: await bcrypt.hash(password, 12),
name,
pic,
})
const savedUser = await User.save()
return savedUser
}catch(error){
throw new Error(error)
}
}
const findUser = async (email)=>{
try{
const user = await User.findOne({ email: email })
return user
}catch(error){
throw new Error(error)
}
}
要记住的关键是你可以 await
on Promise
(async
关键字就像一个装饰器,它强制函数 return a Promise
).
以下方法 return 保证您可以使用 await
User.findOne(...)
bcrypt.hash(...)
user.save()
重构后的代码将如下所示:
async function functionName(...) {
try {
const savedUser = await User.findOne({ email: email });
}
catch (e) {
//...
}
if (savedUser) {
return res
.status(422)
.json({ error: "user already exists with that email" });
}
const hashedpassword = await bcrypt.hash(password, 12)
const user = new User({
email,
password: hashedpassword,
name,
pic,
});
try {
await user.save();
}
catch (e) {
return res
.status(422)
.json({ error: "...ERROR..." });
}
res.json({ message: "saved successfully" });
}