0

We're writing an Extension for WeClapp (https://www.weclapp.com/api2/). Custom Attributes in WeClapp could be added to almost all Datatypes. These Attributes are accessible over a nested JSON object Array.

Interface example:

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;
    ...
} 

Response Example:

{
  "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]",
  ...
}

There is no Problem accessing the IContact (contact in the following example) properties expect the [customAttributes] property. How can I access and manipulate Data?

In the following example contact = IContact:

console.log(contact);

Outputs:

[
  {
    id: '102570',
    ...
    customAttributes: [ [Object], [Object], [Object], [Object] ],
    ...
  }
]
console.log(contact.id); //Outputs: 102570
console.log(contact.customAttributes); //Outputs: undefined

JavaScript Array Extensions (length, ForEach, ...) are not available on [contact.customAttributes], because it's undefined:

let attributes = new Array(0);
attributes = attributes.concat(record.customAttributes);

console.log(attributes); // Outputs: [undefined]

I also tried to Change the interface and tried to reparse:

export interface IContact {
    id: string;
    ...
    customAttributes: string;
    email: string;
    ...
} 

...

let attributes Array<ICustomAttribute>  = JSON.Parse(record.customAttributes);

I have no idea why I can't Access the Array. The strangest thing is that setting attributes does not throw any errors:

let attribute: ICustomAttribute = { attributeDefinitionId: "4576", stringValue: "Test" };
let contact = { customAttributes: [attribute] };

This new entry could be posted and Returns as shown above.

Output of JSON.stringify(contact):

[{"id":"102871",...,"customAttributes":[{"attributeDefinitionId":"4229"},{"attributeDefinitionId":"46381"},{"attributeDefinitionId":"69316"},{"attributeDefinitionId":"98781","stringValue":"77b5d0f1-b1a4-4957-8ea2-ea95969e3c03"}],...,"email":"[email protected]"}]

Output of console.log(contact["customAttributes"]) is undefined.

4
  • How do you get the data from the API? Could you possibly have an unfulfilled promise in there somewhere? Commented Feb 2, 2020 at 12:06
  • i'm runnig a restify server. one endpoints preloads the data in a module in a persistent memory array (Array<IContact>). the other endpoints loops throught this Array (.forEach). i don't use promises in any part of the the project. only npm packages are restify and request (not the promise ones). Commented Feb 2, 2020 at 12:18
  • Try using JSON.stringify to log the IContact so you can see exactly what you're getting. Commented Feb 2, 2020 at 12:49
  • @matt helliwell Added: Output of JSON.stringify(contact): Commented Feb 2, 2020 at 14:08

2 Answers 2

0

Try console.log(contact['customAttributes']);

Sign up to request clarification or add additional context in comments.

1 Comment

result => undefined
0

If your console.log(contact) yields:

[
  {
    id: '102570',
    ...
    customAttributes: [ [Object], [Object], [Object], [Object] ],
    ...
  }
]

Then this suggests that contact.customAttributes does not exist. contact is an array of objects, and to reach the first, you would need contact[0].customAttributes

1 Comment

thx, for reply. i can Access the other properties (see: console.log(contact.id); //Outputs: 102570). so the contact object is not an Collection. am i wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.