I’m trying to create an assocation between a contact and custom object via a custom coded action using Node.
Here’s the code block I’m using:
const createResponse = await hubspotClient.crm.associations.v4.basicApi.create(objectType, objectId, toObjectType, toObjectId, AssociationSpec);
console.log(JSON.stringify(createResponse, null, 2));
} catch (error) {
console.error(error);
}
However, I’m getting the following error when I run the code:
ERROR TypeError: Cannot read properties of undefined (reading 'basicApi')
I pulled the code for the Associations API directly from the dev doc here.
I’m pulling what’s left of my hair out trying to get this figured out. Any ideas on what’s wrong?
Hi @RodWaynick1
The error “TypeError: Cannot read properties of undefined (reading ‘basicApi’)” indicates that the `basicApi` property is not defined in the `hubspotClient.crm.associations.v4` object. This could be due to incorrect usage or a version mismatch in your code.
To resolve this issue, you need to use the correct property and method names for creating an association using the Associations API in HubSpot. Here’s an updated version of your code:
```javascript
try {
const createResponse = await hubspotClient.crm.associationsApi.create({
fromObjectId: objectId,
toObjectId: toObjectId,
fromObjectType: objectType,
toObjectType: toObjectType,
definitionId: AssociationSpec
});
console.log(JSON.stringify(createResponse, null, 2));
} catch (error) {
console.error(error);
}
```
In this updated code, we use the `associationsApi` object instead of `basicApi`. The `create` method expects an object with properties like `fromObjectId`, `toObjectId`, `fromObjectType`, `toObjectType`, and `definitionId` to create the association.
Make sure you have the correct version of the `@hubspot/api-client` package installed in your project. You can check the version in your `package.json` file or use the following command to update it:
```
npm install @hubspot/api-client@latest
```
Remember to replace `hubspotClient` with your actual client instance from the HubSpot API client library you are using.
By making these changes, you should be able to create associations between a contact and a custom object using the Associations API in Node.js successfully.
Hope this will helps you out. Please mark it as Solution Accepted & Upvote to help other Community member.
Thanks!
Oh my god, I’ve been pulling my hair out for hours on this as well! If this is the case, please please just update the documentation. The basicApi is literally in the sample code and apparently it’s not even an existing endpoint then?