如果這很明顯,我深表歉意,因為我還沒有太多的續集經驗。我正在嘗試獲取連接表 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在 type 上不存在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 },
},
},
],
}));
我可以從技術上檢索這些值,但打字稿錯誤讓我停下來。
我很肯定我錯過了一些東西,但無法確定那是什么。任何幫助,將不勝感激!
uj5u.com熱心網友回復:
我相信 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'>
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/408737.html
標籤:
上一篇:正則運算式(節點js)適用于linux但不適用于視窗10
下一篇:登錄授權問題,不通過token
