CMS Development

VJeganathan
Member

Accessing custom object in Serverless function

SOLVE

Hi,

I am having many custom objects and want to access the same in Serverless function. When I try to do like below this,

 

await hubspotClient.crm.objects.basicApi
.getById("mycustomobject", 1234567, ["myproperty"])

 

I am always getting error like " unable to access basicApi of undefined". Can anyone hellp to provide solution to read custom objects in Serverless function.

 

Note: Same code is working fine in Custm code action

 

Thanks

 

 

0 Upvotes
1 Accepted solution
SJaeger
Solution
Contributor | Platinum Partner
Contributor | Platinum Partner

Accessing custom object in Serverless function

SOLVE

hey,

 

why u make it not easy testing/debugging with local nodemon? so u find faster the errors in serverless functions.

create a local index.js file called index.js:

 

 

 

const hubspot = require("@hubspot/api-client");
const API_KEY = 'xxx' // portal api key for testing
const hubspotClient = new hubspot.Client({ apiKey: API_KEY });
const headers = {
  "Content-Type": "application/json"
}

const BASE_URL = 'https://api.sandbox.smartrecruiters.com'


function sendResponse (message) {
  console.log(message)
}

async function serverless (params = {}) {
  const response = await hubspotClient.crm.objects.basicApi.getById("mycustomobject", id, ["myproperty"])
    return response

  sendResponse({
    response,
    statusCode: 200
  });
}

serverless({})

 

 

 

now on your local machine where the index.js is localed type: "npm i nodemon @hubspot/api-client"

after nodemon is installed u can type "nodemon index.js". later u can copy/past this script to your functions folder easily

 

now u see a detailed error messages from hubspot client.

i think your current code have some errors in initialize process of the hubspot client.

 

update: with my example i have to update current hubspot client, because i have the old v2 not current v3

Nothing is impossible

View solution in original post

0 Upvotes
7 Replies 7
piersg
Key Advisor

Accessing custom object in Serverless function

SOLVE

According to the docs remove the "objects" from the call so:

await hubspotClient.crm.basicApi.getById("mycustomobject", 1234567, ["myproperty"]);

0 Upvotes
VJeganathan
Member

Accessing custom object in Serverless function

SOLVE

@piersg , It throws error

ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: Cannot read property 'getById' of undefined","reason":{"errorType":"TypeError","errorMessage":"Cannot read property 'getById' of undefined","stack":["TypeError: Cannot read property 'getById' of undefined"," at Object.exports.main (/var/task/file.js:15:49)"," at Runtime.exports.hubspot_handler [as handler] (/var/task/hubspotHandler.js:6:21)"," at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: Cannot read property 'getById' of undefined"," at process.<anonymous> (/var/runtime/index.js:35:15)"," at process.emit (events.js:314:20)"," at processPromiseRejections (internal/process/promises.js:209:33)"," at processTicksAndRejections (internal/process/task_queues.js:98:32)"]}

0 Upvotes
SJaeger
Solution
Contributor | Platinum Partner
Contributor | Platinum Partner

Accessing custom object in Serverless function

SOLVE

hey,

 

why u make it not easy testing/debugging with local nodemon? so u find faster the errors in serverless functions.

create a local index.js file called index.js:

 

 

 

const hubspot = require("@hubspot/api-client");
const API_KEY = 'xxx' // portal api key for testing
const hubspotClient = new hubspot.Client({ apiKey: API_KEY });
const headers = {
  "Content-Type": "application/json"
}

const BASE_URL = 'https://api.sandbox.smartrecruiters.com'


function sendResponse (message) {
  console.log(message)
}

async function serverless (params = {}) {
  const response = await hubspotClient.crm.objects.basicApi.getById("mycustomobject", id, ["myproperty"])
    return response

  sendResponse({
    response,
    statusCode: 200
  });
}

serverless({})

 

 

 

now on your local machine where the index.js is localed type: "npm i nodemon @hubspot/api-client"

after nodemon is installed u can type "nodemon index.js". later u can copy/past this script to your functions folder easily

 

now u see a detailed error messages from hubspot client.

i think your current code have some errors in initialize process of the hubspot client.

 

update: with my example i have to update current hubspot client, because i have the old v2 not current v3

Nothing is impossible
0 Upvotes
VJeganathan
Member

Accessing custom object in Serverless function

SOLVE

@SJaeger , After fixing my firewall problem it seems working fine. 

I saw network call is triggering internally, will it create problem due to rate limit?

Thanks

0 Upvotes
SJaeger
Contributor | Platinum Partner
Contributor | Platinum Partner

Accessing custom object in Serverless function

SOLVE

@VJeganathan i dont think so. if u not have more then 100 requests per seconds.

Nothing is impossible
0 Upvotes
VJeganathan
Member

Accessing custom object in Serverless function

SOLVE

@SJaeger  It is working fine with the approach you have mentioned above (index.js working fine in nodemon env). But if I paste the code in Design manager and try to execute the endpoint via Postman it thorws error " Execution Time: 722msRuntime.UnhandledPromiseRejection: TypeError: Cannot read property 'basicApi' of undefined". Kindly help me

0 Upvotes
SJaeger
Contributor | Platinum Partner
Contributor | Platinum Partner

Accessing custom object in Serverless function

SOLVE

Hello, do you have a solution? Because on one project I had the same problem and fixed it by axios.get to the HubSpot api.

Nothing is impossible
0 Upvotes