javascript - LocalStrategy 的函数参数在哪里获取它的参数,或者如何使异步函数中的变量在另一个函数中可用?
问题描述
我的代码:
const LocalStrategy = require('passport-local').Strategy;
//called some hash modules
function initialize(passport, getUserByEmail, getUserById) {
var user_copy;
const authenticateUser = async(email, password, done) => {
const userr = await getUserByEmail(email).then(function(userr) {
return userr
})
user_copy = userr; //I can't reach userr so I create copy of it on accessible variable.
if (userr == null) {
return done(null, false, {
message: "No user with this email..."
});
}
try {
//hashing part
} catch (e) {
console.log(e);
return done(e);
}
}
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
}, authenticateUser));
/////////////////////Problem/////////////////////
passport.serializeUser((user_copy, done) => done(null, user_copy[0]._id)); //In here I get error( at the under line too). It see user_copy undefined.
passport.deserializeUser((id, done) => {
return done(null, getUserById(user_copy[0]._id));
})
}
/////////////////////////////////////////////////
module.exports = initialize
实际上,如果我能够在框外调用 getUserByEmail 就没有问题,但我不知道 authenticateUser 从哪里获取电子邮件参数,所以我尝试在 authenticateUser 中完成我的工作,但找不到解决方案。
我在哪里调用初始化:
initializePassport(
passport,
async function email(email) {
await client.connect();
const db = client.db(dbName);
const collection = db.collection(collectionName);
found = await collection.find({ email: email }).toArray().then(user => { return user });
client.close();
return found;
},
async function id() {
await client.connect();
const db = client.db(dbName);
const collection = db.collection(collectionName);
found = await collection.find({ id: user.id }).toArray().then(user => { return user });
client.close();
return found;
}
);
我只想能够访问 userr 或它的任何副本。我尝试了太多方法,但找不到任何方法。我控制一切的另一件事没有问题。
解决方案
您应该做的是将user_copy
变量分配给的返回值getUserByEmail
const LocalStrategy = require('passport-local').Strategy;
//called some hash modules
function initialize(passport, getUserByEmail, getUserById) {
var user_copy;
const authenticateUser = async(email, password, done) => {
user_copy = await getUserByEmail(email).then(function(userr) {
return userr
})
// this wouldn't be needed
//user_copy = userr; //I can't reach userr so I create copy of it on accessible variable.
if (user_copy == null) {
return done(null, false, {
message: "No user with this email..."
});
}
try {
//hashing part
} catch (e) {
console.log(e);
return done(e);
}
}
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
}, authenticateUser));
/////////////////////Problem/////////////////////
passport.serializeUser((user_copy, done) => done(null, user_copy[0]._id)); //In here I get error( at the under line too). It see user_copy undefined.
passport.deserializeUser((id, done) => {
return done(null, getUserById(user_copy[0]._id));
})
}
/////////////////////////////////////////////////
module.exports = initialize
推荐阅读
- sql - Oracle - 加入逗号分隔的数据
- javascript - 如何修复未捕获的 ReferenceError:未定义缩略图 - 使用以下代码
- python - Seaborn/Matplotlib:在水平条形图上绘制过滤数据
- selenium - 在 Selenium 或 UI.Vision 上使用 cy.clock() 和 cy.tick() 之类的东西
- bash - 在 bash 脚本中使用制表符补全
- git - Git 合并“到”还是合并“到”?
- c++ - C++ 指针与对象
- javascript - 辅助功能 - aria-haspopup 菜单 - 菜单中除 menuitem 之外的交互式元素
- pandas - 特定日期 timedelta 之间的 Pandas groupby ID
- node.js - req.body 选项选择 express ejs