GET data inside serverless function

Hello,

I have a website built with Gatsby/React and am trying to create a CMS with Hubspot to manage the blog. I’ve tried the Gatsby/Hubspot plugin for this but the problem is that it will only do a GraphQL query on each build. So if new content is created in Hubspot, I would need to manually rebuild the site each time. That led me to reading about serverless functions in Hubspot and the idea of getting all of the blog posts from Hubspot on page load rather than the single query. I’ve followed these guides:

https://community.hubspot.com/t5/APIs-Integrations/How-to-fix-cors-is-not-allowing-API-request-issue/m-p/459610

I cannot figure out how to get data from an api endpoint from my serverless function and return it. I keep getting 500 errors when I hit my endpoint and errors like this:

Runtime.UnhandledPromiseRejection: TypeError: Cannot read property ‘data’ of undefined.

and

UNHANDLED_ERROR - Execution Time: 445msRuntime.UnhandledPromiseRejection: TypeError: sendResponse is not a function

Here is my serverless.json:

{

“runtime”: “nodejs12.x”,

“version”: “1.0”,

“endpoints”: {

“congratulation”: {

“method”: “GET”,

“file”: “congratulation.js”

},

“getBlogs”: {

“method”: “GET”,

“file”: “getBlogs.js”

},

“getDogs”: {

“method”: “GET”,

“file”: “getDogs.js”

}

}

}

Here is getDogs.js:

exports.main = (sendResponse) => {

const getBreeds = async () => {

try {

return await axios.get(‘https://dog.ceo/api/breeds/list/all’)

} catch (error) {

console.error(error)

}

}

const countBreeds = async () => {

const breeds = await getBreeds()

if (breeds) {

sendResponse({body: “success”, statusCode: 200});

console.log(breeds)

}

}

countBreeds()

}

Any help would be greatly appreciated. Thanks!

@Mark_Ryba , maybe you have some thoughts on this :grinning_face: