coupon.getItemTypes()不是belongsToMany的函数

问题描述 投票:0回答:1

我正在使用续集v5。我在many-to-manyCoupon之间有ItemType关系虽然CouponItemTypecoupon.getItemTypes()给出类型错误

TypeError:coupon.getItemTypes不是函数

const coupon = await Coupon.findOne({
        where: {
            id: couponId,
            is_active: true
        },
        attributes: {
            exclude: ["created_by", "updated_by", "created_on", "updated_on"]
        }
    })
const validForType = await coupon.getItemTypes({
      where: {
          item_type_id: 1
      },
      attributes: ["id"]
    })

Coupon.js

 Coupon.associate = function(models) {
    // associations can be defined here
    Coupon.belongsToMany(models.ItemType, {
      through: 'CouponItemType',
      as: 'coupon_type',
      foreignKey: 'coupon_id',
      otherKey: 'item_type_id'
    })
  };

ItemType.js

 ItemType.associate = function(models) {
    // associations can be defined here
    ItemType.belongsToMany(models.Coupon, {
      through: 'CouponItemType',
      as: 'coupon_item_types',
      foreignKey: 'item_type_id',
      otherKey: 'coupon_id'
    })
  };

CouponItemType.js

'use strict';
module.exports = (sequelize, DataTypes) => {
  const CouponItemType = sequelize.define('CouponItemType', {
    coupon_id:DataTypes.BIGINT,
    item_type_id:DataTypes.SMALLINT,
    created_by: DataTypes.STRING,
    updated_by: DataTypes.STRING,
    created_on: DataTypes.DATE,
    updated_on: DataTypes.DATE
  }, {
    tableName: 'coupon_item_types'
  });
  CouponItemType.associate = function(models) {
    // associations can be defined here
  };
  return CouponItemType;
}

尽管有充分的文献here

javascript node.js orm sequelize.js
1个回答
1
投票

简短回答,因为您使用别名(as)选项。该语句为as: 'coupon_type',您需要使用Coupon.getCoupon_type()来获取ItemTypeCoupon

答案很长,这是一个有效的示例:

index.ts

import { sequelize } from '../../db';
import { Model, DataTypes } from 'sequelize';

class Coupon extends Model {}
Coupon.init(
  {
    coupon_id: {
      unique: true,
      type: DataTypes.BIGINT,
    },
  },
  { sequelize, modelName: 'Coupons' },
);

class ItemType extends Model {}
ItemType.init(
  {
    item_type_id: {
      unique: true,
      type: DataTypes.SMALLINT,
    },
  },
  { sequelize, modelName: 'ItemTypes' },
);

class CouponItemType extends Model {}
CouponItemType.init(
  {
    coupon_id: DataTypes.BIGINT,
    item_type_id: DataTypes.SMALLINT,
    created_by: DataTypes.STRING,
    updated_by: DataTypes.STRING,
    created_on: DataTypes.DATE,
    updated_on: DataTypes.DATE,
  },
  { sequelize, modelName: 'CouponItemType' },
);

Coupon.belongsToMany(ItemType, {
  through: 'CouponItemType',
  as: 'coupon_type',
  foreignKey: 'coupon_id',
  otherKey: 'item_type_id',
});

ItemType.belongsToMany(Coupon, {
  through: 'CouponItemType',
  as: 'coupon_item_types',
  foreignKey: 'item_type_id',
  otherKey: 'coupon_id',
});

(async function test() {
  try {
    await sequelize.sync({ force: true });
    const couponDataRecords = [
      { coupon_id: 1, coupon_type: [{ item_type_id: 1 }, { item_type_id: 2 }] },
      { coupon_id: 2, coupon_type: [{ item_type_id: 4 }, { item_type_id: 3 }] },
    ];
    await Coupon.bulkCreate(couponDataRecords, {
      include: [
        {
          model: ItemType,
          as: 'coupon_type',
        },
      ],
    });

    const coupon = await Coupon.findOne({ where: { coupon_id: 1 } });
    const validForType = await coupon.getCoupon_type({
      where: {
        item_type_id: 1,
      },
      attributes: ['id'],
      raw: true,
    });
    console.log('validForType: ', validForType);
  } catch (error) {
    console.log(error);
  } finally {
    await sequelize.close();
  }
})();

以上代码的执行结果:

validForType:  [ { id: 1,
    'CouponItemType.coupon_id': 1,
    'CouponItemType.item_type_id': 1,
    'CouponItemType.created_by': null,
    'CouponItemType.updated_by': null,
    'CouponItemType.created_on': null,
    'CouponItemType.updated_on': null } ]

检查数据库中的数据记录:

node-sequelize-examples=# select * from "Coupons";
 id | coupon_id
----+-----------
  1 |         1
  2 |         2
(2 rows)

node-sequelize-examples=# select * from "ItemTypes";
 id | item_type_id
----+--------------
  1 |            1
  2 |            2
  3 |            4
  4 |            3
(4 rows)

node-sequelize-examples=# select * from "CouponItemType";
 coupon_id | item_type_id | created_by | updated_by | created_on | updated_on
-----------+--------------+------------+------------+------------+------------
         1 |            1 |            |            |            |
         1 |            2 |            |            |            |
         2 |            3 |            |            |            |
         2 |            4 |            |            |            |
(4 rows)

db.ts

const sequelize = new Sequelize({
  dialect: 'postgres',
  host: envVars.POSTGRES_HOST,
  username: envVars.POSTGRES_USER,
  password: envVars.POSTGRES_PASSWORD,
  database: envVars.POSTGRES_DB,
  port: Number.parseInt(envVars.POSTGRES_PORT, 10),
  define: {
    freezeTableName: true,
    timestamps: false,
  },
});

序列化版本:"sequelize": "^5.21.3"

源代码:https://github.com/mrdulin/node-sequelize-examples/tree/master/src/examples/stackoverflow/60449016

© www.soinside.com 2019 - 2024. All rights reserved.