HubSpot API error: POST fails with 'Cannot read properties of undefined (reading 'makeRequestContext'), GET works

334 views Asked by At

I encountered an error while attempting to use the HubSpot api-client plugin and setting the Nuxt.js proxy. Specifically, I received the error message 'Cannot read properties of undefined (reading 'makeRequestContext')' when attempting to make a POST request, whereas GET requests worked correctly.

Is there someone who can help me solve this problem?

This is my code:

I set the nuxt proxy in nuxt.config.js:

proxy: {
    '/hubspot/': {
      target: process.env.HUBSPOT_API_URL,
      pathRewrite: {
        '^/hubspot/': '/'
      },
      changeOrigin: true
    }
  },
  axios: { proxy: true },
  modules: { '@nuxtjs/proxy' }

And then, I create a file under the services folder:

export default ({ store: { state }, app: { $config } }, inject) => {
  const { Client } = require('@hubspot/api-client')

  const hubSpotClient = new Client({
    accessToken: $config.hubspotAccessToken,
    portalId: $config.hubspotPortalId,
    basePath: 'http://localhost:8080/hubspot'
  })

  const requestOptions = {
    headers: {
      'Content-Type': 'application/json',
      Accept: 'application/json'
    }
  }
  const hubspotContactId = state.account.organization.hubspotContactId
  const readContactInfo = async ({ email = '' }) => {
    try {
      const filterData = {
        filterGroups: [
          {
            filters: [
              {
                value: '[email protected]',
                propertyName: 'email',
                operator: 'EQ'
              }
            ]
          }
        ]
      }
      const contactInfo = await hubSpotClient.crm.contacts.searchApi.doSearch(
        filterData,
        requestOptions
      )
    } catch (error) {
      throw new Error('Get contact info failed.')
    }
  }

  

  const readTickets = async () => {
    try {
      const res = await hubSpotClient.crm.tickets.basicApi.getPage()
    } catch (error) {
      throw new Error('Reading ticket error.')
    }
  }

  inject('hubSpot', {
    readContactInfo,
    readTickets
  })
}

By the way, if I don't send the 'requestOptions' when using the POST method, I get an error message '504 Gateway Timeout' with the additional error message of 'No Content-Type'.

const requestOptions = {
    headers: {
      'Content-Type': 'application/json',
      Accept: 'application/json'
    }
}

Expectation: Get the contact id by email.

0

There are 0 answers