【发布时间】:2021-02-08 21:43:45
【问题描述】:
我已经为我的项目设置了护照和 sequelize-typescript。在护照设置中,我使用了这样的策略,例如 google:
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_AUTH_CLIENT_ID,
clientSecret: process.env.GOOGLE_AUTH_CLIENT_SECRET,
callbackURL: process.env.GOOGLE_AUTH_CALLBACK_URL,
profileFields: ['id', 'displayName', 'photos', 'email'],
enableProof: true
},
function(accessToken, refreshToken, profile, done) {
console.log(profile)
const { name, email, picture } = profile._json;
User.findOne({where: {id: profile.id}})
.then(user => {
console.log(user)
if(user === null) {
const { name, email, picture } = profile._json;
// new User({
// id: profile.id,
// name: name,
// email: email,
// pictureUrl: picture,
// })
}
})
done(null, profile)
}
)
)
当我尝试使用 findOrCreate() 或 findOne() 等函数时,我收到一个打字稿错误,内容如下:
[ERROR] 23:29:01 ⨯ Unable to compile TypeScript:
src/passport_strategies.ts:45:18 - error TS2339: Property 'findOne' does not exist on type 'typeof User'.
45 User.findOne({where: {id: profile.id}})
对于第一个代码 sn-p 中注释掉的部分,我也遇到了同样的错误。我创建的模型用户声明如下:
export class User extends Model<User> {}(在文件中设置了列)Model从sequelize-typescript导入
这里是创建 sequelize 的地方:
export const sequelize = new Sequelize({
"username": c.username,
"password": c.password,
"database": c.database,
"host": c.host,
dialect: 'postgres',
storage: ':memory:',
models: [__dirname + '/models']
});
我尝试检查互联网上的其他示例,但它们都具有相同的设置,我无法弄清楚为什么会出现此错误。不确定这是否有帮助,但我使用的是 postgres 方言。
【问题讨论】:
标签: express sequelize.js passport.js sequelize-typescript