为什么我会收到“TypeError:无法读取未定义的属性‘Sequelize’”

时间:2021-06-27 11:25:19

标签: javascript node.js postgresql orm sequelize.js

我在一个项目中使用带有 TypeScript 的 Sequelize。我的 Sequelize 版本是 6.6.2。而且我使用的是 PostgreSQL 方言。

我正在尝试创建两个模型:RestaurantFoodCategory

这是我的餐厅模型文件:

import { sequelize } from ".";
import { Sequelize, Model, DataTypes, Optional } from "sequelize";

import FoodCategory from './FoodCategory.model'

interface RestaurantAttributes {
    restaurantId: string
    name: string
    bannerImage: string
    street: string
    city: string
    state: string
    country: string
    rating: number
    logo?: string
    isVerified: boolean
}
interface RestaurantCreationAttributes extends Optional<RestaurantAttributes, 'restaurantId'> {}

export default class Restaurant extends Model<RestaurantAttributes, RestaurantCreationAttributes> implements RestaurantAttributes {
    /**
     * This class contains properties and methods specific to the Restaurant model
     */
    // Required attributes on model creation
    restaurantId!: string
    name!: string
    bannerImage!: string
    street!: string
    city!: string
    state!: string
    country!: string
    rating!: number
    isVerified!: boolean
    // Optional attributes on model creation
    logo?: string
}

Restaurant.hasMany(FoodCategory, { as: 'foodCategories' })
FoodCategory.belongsTo(Restaurant, { as: 'restaurant', foreignKey: { name: 'restaurantId', allowNull: false,  } })

Restaurant.init({
    restaurantId: {
        type: DataTypes.UUID,
        primaryKey: true,
        defaultValue: DataTypes.UUIDV4,
        allowNull: false
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
    logo: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null
    },
    bannerImage: {
        type: DataTypes.STRING,
        allowNull: false
    },
    street: {
        type: DataTypes.STRING,
        allowNull: false
    },
    city: {
        type: DataTypes.STRING,
        allowNull: false
    },
    state: {
        type: DataTypes.STRING,
        allowNull: false
    },
    country: {
        type: DataTypes.STRING,
        allowNull: false
    },
    rating: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0
    },
    isVerified: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false
    }
}, {
    sequelize,
    tableName: 'Restaurant'
})

这是我的 FoodCategory 模型文件:

import { sequelize } from '.'
import { Model, DataTypes, Optional } from 'sequelize'
import Restaurants from './Restaurant.model'

interface FoodCategoryAttributes {
    foodCategoryId: number
    restaurantId: string
    name: string
}
interface FoodCategoryCreationAttributes extends Optional<FoodCategoryAttributes, 'foodCategoryId'> {}

export default class FoodCategory extends Model<FoodCategoryAttributes, FoodCategoryCreationAttributes> implements FoodCategoryAttributes {
    // Required attributes on model creation
    foodCategoryId!: number
    name!: string
    restaurantId!: string
    // Possible attributes when associations are formed
    restaurant?: Restaurant
}

FoodCategory.init({
    foodCategoryId: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
        allowNull: false
    },
    restaurantId: {
        type: DataTypes.UUID,
        allowNull: false
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
}, {
    sequelize,
    tableName: 'FoodCategory',
})

我正在建立 Restaurant 表和 FoodCategory 表之间的一对多关系(如 Restaurant 模型文件中所示)。因此,Restaurant 表中的一个条目可以在 FoodCategory 表中链接多个条目。而一个 FoodCategory 只属于一个 Restaurant

我能够在 RestaurantFoodCategory 表中成功创建一个条目。但是当我尝试查询 FoodCategory 表并将其加入 Restaurant 表时,如下所示:

const response = await FoodCategory.findOne({
   where: {
     restaurantId: '9c349fc4-af71-41a0-aea3-7a19d0bb498e' // A restaurant exists with this id
   },
   include: Restaurant
})
console.log(response)

我在控制台中收到以下错误:

C:\Users\HP\Desktop\api\node_modules\sequelize\lib\associations\mixin.js:12
    && model.prototype instanceof sequelize.Sequelize.Model;
                                            ^
TypeError: Cannot read property 'Sequelize' of undefined
    at isModel (C:\Users\HP\Desktop\api\node_modules\sequelize\lib\associations\mixin.js:12:45)
    at Function.hasMany (C:\Users\HP\Desktop\api\node_modules\sequelize\lib\associations\mixin.js:17:10)
    at Object.<anonymous> (C:\Users\HP\Desktop\api\src\database\models\Restaurant.model.ts:53:12)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
....

所以问题确实是我无法将两个表连接在一起。请我想知道此错误的可能原因。谢谢。

0 个答案:

没有答案