也许有人可以帮我解决这个问题:
我们正在为WeClapp(https://www.weclapp.com/api2/)编写扩展程序。 WeClapp中的自定义属性可以添加到几乎所有数据类型中。这些属性可通过嵌套的JSON对象数组访问。
接口示例:
export interface ICustomAttribute {
attributeDefinitionId: string;
booleanValue?: boolean;
dateValue?: number;
entityId?: string;
numberValue?: number;
selectedValueId?: string;
stringValue?: string;
}
export interface IContact {
id: string;
...
customAttributes: ICustomAttribute[];
email: string;
...
}
响应示例:
{
"id": "4317",
...
"customAttributes": [
{
"attributeDefinitionId": "4576",
"booleanValue": null,
"dateValue": null,
"entityId": null,
"numberValue": null,
"selectedValueId": null,
"selectedValues": null,
"stringValue": "Test"
},
{
"attributeDefinitionId": "69324",
"booleanValue": true,
"dateValue": null,
"entityId": null,
"numberValue": null,
"selectedValueId": null,
"selectedValues": null,
"stringValue": null
}
],
...
"email": "[email protected]",
...
}
访问IContact(在以下示例中为联系人)属性没有问题,需要[customAttributes]属性。问题是如何访问和操纵数据。
在以下示例中,contact = IContact:
console.log(contact);
输出:
[
{
id: '102570',
...
customAttributes: [ [Object], [Object], [Object], [Object] ],
...
}
]
console.log(contact.id); //Outputs: 102570
console.log(contact.customAttributes); //Outputs: undefined
JavaScript数组扩展(长度,ForEach,...)在[contact.customAttributes]上不可用,因为ist未定义
let attributes = new Array(0);
attributes = attributes.concat(record.customAttributes);
console.log(attributes); // Outputs: [undefined]
我也尝试更改界面并尝试重新解析:
export interface IContact {
id: string;
...
customAttributes: string;
email: string;
...
}
...
let attributes Array<ICustomAttribute> = JSON.Parse(record.customAttributes);
我不知道为什么我们不能访问数组?
最奇怪的是设置属性没有问题:
let attribute: ICustomAttribute = { attributeDefinitionId: "4576", stringValue: "Test" };
let contact = { customAttributes: [attribute] };
此新条目可以过帐并返回,如上所示。
对不起拼写问题,但我不是英语母语。而且,此问题的原因,目前相当混乱;)
JSON.stringify(contact)的输出:
[{"id":"102871",...,"customAttributes":[{"attributeDefinitionId":"4229"},{"attributeDefinitionId":"46381"},{"attributeDefinitionId":"69316"},{"attributeDefinitionId":"98781","stringValue":"77b5d0f1-b1a4-4957-8ea2-ea95969e3c03"}],...,"email":"[email protected]"}]
console.log(contact [“ customAttributes”])的输出=>未定义
尝试console.log(contact['customAttributes']);