【发布时间】:2021-06-08 06:49:48
【问题描述】:
我正在使用 Passport JS 本地策略,并想添加一个自定义函数,对用户提供的电子邮件进行一些验证
async function checkEmail(email) {
const url = `https://endpoint-to-url/${email}`;
await fetch(url)
.then(response => response.json())
.then(async json => {
let result;
if (json.status === 200 && json.temporary === true) {
result = true;
} else {
result = false;
}
return result;
})
.catch(async function(error) {
console.log(`Error whilst checking email ${error}`);
});
}
护照 JS
passport.use(
'local-signup',
new LocalStrategy(
{
usernameField: 'signup_email',
passwordField: 'signup_password',
passReqToCallback: true,
},
async (req, email, password, done) => {
process.nextTick(async () => {
const isTempEmail = await checkEmail(req.body.signup_email);
if (isTempEmail === true) {
return done(null, false, { errors: 'Unable to use this email' });
}
});
}
)
);
每次isTempEmail 返回undefined,但如果我注销checkEmail(email) 末尾返回的是true 或false。
我觉得我在混合回调和异步/等待功能,想知道我哪里出错了。
【问题讨论】:
标签: javascript async-await passport.js