【问题标题】:cannot use the find functions using sequelize-typescript无法使用使用 sequelize-typescript 的查找功能
【发布时间】: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> {}(在文件中设置了列)Modelsequelize-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


    【解决方案1】:

    我怀疑是版本不匹配。 sequelize-typescript@2 用于 sequelize@6.2>=,sequelize-typescript@1 用于 sequelize@5>=。

    出于教育目的,我还建议在不使用 sequelize-typescript 包的情况下使用 sequelize 实现 typescript,只是为了了解包本身的需求。 https://sequelize.org/master/manual/typescript.html

    为了以防万一,我指出如果您使用的是最新版本,则需要@Table。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多