gpt4 book ai didi

javascript - coupon.getItemTypes() 不是属于ToMany 的函数

转载 作者:行者123 更新时间:2023-12-02 21:37:45 25 4
gpt4 key购买 nike

我正在使用sequelize v5。我在 CouponItemType 之间存在多对多关系尽管CouponItemType

coupon.getItemTypes() 给出类型错误

TypeError: coupon.getItemTypes is not a function

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"]
})


优惠券.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

最佳答案

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

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

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 版本:"sequelize": "^5.21.3"

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

关于javascript - coupon.getItemTypes() 不是属于ToMany 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60449016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com