Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Issue when trying to Remove an existing contact from a list (client.lists.removeContacts(listId, { vids: [conactId] })) #268

Open
mrdntgrn opened this issue Mar 17, 2020 · 0 comments

Comments

@mrdntgrn
Copy link
Contributor

Hi everyone,

We use Nest.js framework as backend and we also integrated Hubspot service by using this open source pretty library. First of all thank you.

The Problem is that we have to call two times the Nest.js API, which we created to remove an contact from list. Under this API controller/action I have the code which uses the service similar the following class(it is written on Typescript and there are two placeholders {MY_API_KEY_HERE} and {MY_CONTACT_LIST_ID_HERE}).

// file hubspot.service.ts
import Hubspot from 'hubspot'

export class HubspotService {
    protected _client = null
    public apiKey = '{MY_API_KEY_HERE}'
    public listId = '{MY_CONTACT_LIST_ID_HERE}'

    public get client(): Hubspot {
        if (!this._client) {
            this._client = new Hubspot({
                apiKey: this.apiKey
            })
        }

        return this._client
    }

    public async removeContactFromList(conactId: number) {
        try {
            let response = await this.client.lists.removeContacts(this.listId, { vids: [conactId] })
            console.log('Successful Hubspot remove contact from list response: ', response)

        } catch (e) {
            console.log(`Hubspot contact remove contact from list error: ${e.message}`)
            throw e
        }
    }

    public async addContactToList(conactId: number) {
        try {
            let response = await this.client.lists.addContacts(this.listId, { vids: [conactId] })
            console.log('Successful Hubspot add contact to list response: ', response)

        } catch (e) {
            console.log(`Hubspot contact add to list error: ${e.message}`)
            throw e
        }
    }
}

here is what I'm getting as response in console after the text 'Successful Hubspot remove contact from list response: ' when I calling the removeContactFromList(conactId: number) above function by using conactId=1951:

{
    "updated": [
        1951
    ],
    "discarded": [],
    "invalidVids": [],
    "invalidEmails": []
}

The fact is that not every time(as I noticed about every time) this function single call do not remove the contact from the list and just after second API call the contact gets removed from list.

I tried direct Hubspot API calls by using curl and the contact getting removed from contact list after single call:

curl --location --request POST 'https://api.hubapi.com/contacts/v1/lists/{MY_CONTACT_LIST_ID_HERE}/remove?hapikey={MY_API_KEY_HERE}' \
--header 'Content-Type: application/json' \
--data-raw '{
  "vids": [
    1951
  ]
}'

The interesting moment is that I have also addContactToList(conactId: number) function in my Typescript class and it works normally just after first call.

Please if any one can have any idea what the issue is, I will be very happy, so that there will not be need to have to double call the contact remove from list backend API two times (this is my workaround for now to fix the issue manually). Let me know if you need more information on this.

Thank you

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant