无法正确访问联结 table 上的其他字段
Unable to properly access additional fields on junction table
如果这很明显,我深表歉意,因为我对 sequelize 还没有太多经验。我正在尝试获取位于联结 table CustomerContact 中的字段。目前我正在获取联系人,然后检索其关联的客户
const contactList: Contact[] = await db()
.Contact.newActiveScope()
.withCustomers()
.findAll();
const mainCustomer: Customer = contactList[i].getMainCustomer();
const mainCustomerContacts = mainCustomer && mainCustomer?.customerContacts;
const thisCustomerContactRole = mainCustomerContacts?.find((c) => c.contactId === contact.id)?.role;
输出 mainCustomer
得到以下信息
{
id: 'a94fd13a-1fdd-11ec-a12c-121c563gb2f5',
accountName: 'Test Tests',
CustomerContact: { role: 1, relationship: 2, status: 'active' }
}
我想使用 CustomerContact
中的属性,但尝试访问它时出现打字稿错误,提示 CustomerContact
在类型 ContactWithCustomerContactStatic
上不存在,它具有以下内容形状。
export type ContactWithCustomerContactStatic = Partial<Customer> &
Pick<CustomerContact, 'role' | 'relationship'>;
我在联系人模型中关联客户
this.belongsToMany(Customer, {
as: 'customers',
foreignKey: 'contactId',
otherKey: 'customerId',
through: CustomerContact as CustomerContactStatic,
});
this.addScope('withCustomers', {
include: [
{
as: 'customers',
model: Customer as CustomerStatic,
where: { isArchived: false },
required: false,
},
],
});
反之亦然
Customer.belongsToMany(Contact, {
as: 'contacts',
through: CustomerContact,
foreignKey: 'customerId',
otherKey: 'contactId',
});
Customer.addScope('contacts', () => ({
include: [
{
as: 'contacts',
attributes: ['id', 'firstName', 'lastName'],
model: (Contact as ContactStatic).newScope(),
required: false,
through: {
where: { status: CustomerContactStatus.active },
},
},
],
}));
技术上我可以检索这些值,但打字稿错误让我停顿了一下。
我确定我遗漏了一些东西,但无法确定那是什么。任何帮助将不胜感激!
我认为 Typescript 显示此错误是因为您声明 ContactWithCustomerContactStatic 类型的方式。
export type ContactWithCustomerContactStatic = Partial<Customer> &
Pick<CustomerContact, 'role' | 'relationship'>;
以这种方式声明时,您对 Typescript 说 ContactWithCustomerContactStatic 类型具有以下形状
interface ContactWithCustomerContactStatic {
// all properties from Customer interface comes here
role // it's in the root
relationship // it's also in the root
}
但是,鉴于您提供的输出,它们不在对象的根目录中
{
id: 'a94fd13a-1fdd-11ec-a12c-121c563gb2f5',
accountName: 'Test Tests',
CustomerContact: { role: 1, relationship: 2, status: 'active' }
}
这应该有效
type ContactWithCustomerContactStatic = Partial<Customer>
& { customerContact: Pick<CustomerContact, 'role' | 'relationship'>
}
如果这很明显,我深表歉意,因为我对 sequelize 还没有太多经验。我正在尝试获取位于联结 table CustomerContact 中的字段。目前我正在获取联系人,然后检索其关联的客户
const contactList: Contact[] = await db()
.Contact.newActiveScope()
.withCustomers()
.findAll();
const mainCustomer: Customer = contactList[i].getMainCustomer();
const mainCustomerContacts = mainCustomer && mainCustomer?.customerContacts;
const thisCustomerContactRole = mainCustomerContacts?.find((c) => c.contactId === contact.id)?.role;
输出 mainCustomer
得到以下信息
{
id: 'a94fd13a-1fdd-11ec-a12c-121c563gb2f5',
accountName: 'Test Tests',
CustomerContact: { role: 1, relationship: 2, status: 'active' }
}
我想使用 CustomerContact
中的属性,但尝试访问它时出现打字稿错误,提示 CustomerContact
在类型 ContactWithCustomerContactStatic
上不存在,它具有以下内容形状。
export type ContactWithCustomerContactStatic = Partial<Customer> &
Pick<CustomerContact, 'role' | 'relationship'>;
我在联系人模型中关联客户
this.belongsToMany(Customer, {
as: 'customers',
foreignKey: 'contactId',
otherKey: 'customerId',
through: CustomerContact as CustomerContactStatic,
});
this.addScope('withCustomers', {
include: [
{
as: 'customers',
model: Customer as CustomerStatic,
where: { isArchived: false },
required: false,
},
],
});
反之亦然
Customer.belongsToMany(Contact, {
as: 'contacts',
through: CustomerContact,
foreignKey: 'customerId',
otherKey: 'contactId',
});
Customer.addScope('contacts', () => ({
include: [
{
as: 'contacts',
attributes: ['id', 'firstName', 'lastName'],
model: (Contact as ContactStatic).newScope(),
required: false,
through: {
where: { status: CustomerContactStatus.active },
},
},
],
}));
技术上我可以检索这些值,但打字稿错误让我停顿了一下。
我确定我遗漏了一些东西,但无法确定那是什么。任何帮助将不胜感激!
我认为 Typescript 显示此错误是因为您声明 ContactWithCustomerContactStatic 类型的方式。
export type ContactWithCustomerContactStatic = Partial<Customer> &
Pick<CustomerContact, 'role' | 'relationship'>;
以这种方式声明时,您对 Typescript 说 ContactWithCustomerContactStatic 类型具有以下形状
interface ContactWithCustomerContactStatic {
// all properties from Customer interface comes here
role // it's in the root
relationship // it's also in the root
}
但是,鉴于您提供的输出,它们不在对象的根目录中
{
id: 'a94fd13a-1fdd-11ec-a12c-121c563gb2f5',
accountName: 'Test Tests',
CustomerContact: { role: 1, relationship: 2, status: 'active' }
}
这应该有效
type ContactWithCustomerContactStatic = Partial<Customer>
& { customerContact: Pick<CustomerContact, 'role' | 'relationship'>
}