简体   繁体   English

Express typescript 中的“typeof db”类型不存在属性“用户”

[英]Property 'User' does not exist on type 'typeof db' sequelize in express typescript

I am using sequelize with express typescript.我正在使用带有 express typescript 的续集。

I was use sequelize-cli to generate User model.我使用 sequelize-cli 生成用户 model。 When I tried to get User from model.当我试图从 model 获取用户时。

I get an error: Property 'User' does not exist on type 'typeof db'我收到一个错误: Property 'User' does not exist on type 'typeof db'

How can I access to User model inside my database model.如何在我的数据库 model 中访问用户 model。

Thank you for your help.谢谢您的帮助。

This is my code.这是我的代码。

model/index.js模型/index.js

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(
    config.database,
    config.username,
    config.password,
    config
  );
}

fs.readdirSync(__dirname)
  .filter((file) => {
    return (
      file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js'
    );
  })
  .forEach((file) => {
    const model = require(path.join(__dirname, file))(
      sequelize,
      Sequelize.DataTypes
    );
    db[model.name] = model;
  });

Object.keys(db).forEach((modelName) => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

model/user.js模型/user.js

'use strict';
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class User extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  }
  User.init(
    {
      name: DataTypes.STRING,
      email: DataTypes.STRING,
      password: DataTypes.STRING,
    },
    {
      sequelize,
      modelName: 'User',
    }
  );
  return User;
};

repository/auth.ts存储库/auth.ts

import DB from '../../database/models';

const { User } = DB; // Property 'User' does not exist on type 'typeof db'

You can use any to fix this error:您可以使用any来修复此错误:

import db from '../../database/models';

const DB: any = db;
const { User } = DB;

On file model/index.js在文件model/index.js

You can change module.exports = db;您可以更改module.exports = db; to export default db; export default db;

Then you can import like this:然后你可以像这样导入:

import db from '../../database/models';

const { User } = db;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿编译错误:快递类型“typeof e”上不存在属性“raw” - Typescript compile error: Property 'raw' does not exist on type 'typeof e' on express Typescript编译错误:类型'typeof e'不存在属性'bodyParser' - Typescript compile error: Property 'bodyParser' does not exist on type 'typeof e' Typescript Node JS - 类型“Global & typeof globalThis”上不存在“属性” - Typescript Node JS - `Property does not exist on type 'Global & typeof globalThis'` (Express、Node、TypeScript、REST API)如何将函数从服务导入到控制器? 属性...在类型 typeof 上不存在 - (Express, Node, TypeScript, REST API) How can I import a function from the service to the controllers? Property ... does not exist on type typeof typescript 在类型'typeof object 上不存在 - typescript does not exist on type 'typeof object TypeScript + Express + Passport.js - 用户类型上不存在属性 ID - TypeScript + Express + Passport.js - Property id does not exist on type User 获取错误属性'...'在类型'typeof'上不存在...“' - getting error Property '…' does not exist on type 'typeof “…”' 类型'typeof Schema'上不存在属性'virtual' - Property 'virtual' does not exist on type 'typeof Schema' 类型“typeof”上不存在属性“LambdaProxyIntegration” - Property 'LambdaProxyIntegration' does not exist on type 'typeof' 类型'typeof import 上不存在属性'connect' - Property 'connect' does not exist on type 'typeof import
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM