我已经为我的项目设置了passport和sequelize-typescript。在passport设置中,我使用了一个策略,例如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()之类的函数时,我收到一个typescript错误,显示如下:
[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}})对于第一个代码片段中注释掉的部分,我也得到了相同的错误。我创建的模型用户是这样声明的:export class User extends Model<User> {} (在文件中设置了列)从sequelize-typescript导入的Model
下面是创建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方言。
发布于 2021-02-08 21:43:45
我怀疑这是一个版本不匹配。sequelize-typescript@2用于sequelize@6.2>=,sequelize-typescript@1用于sequelize@5>=。
出于教育目的,我还建议使用sequelize实现typescript,而不使用sequelize-typescript包,这只是为了理解包本身的需求。https://sequelize.org/master/manual/typescript.html
另外,为了以防万一,我指出,如果您使用的是最新版本,则需要@Table。
https://stackoverflow.com/questions/66073638
复制相似问题