APIs & Integrations

MMartin78
Membro

Using fetch() instead of requests library in custom action?

I'm getting an "unhandled promise rejection" error in my workflow's custom JS action, and I cannot fathom why. As far as I can tell, there is no "promise" returned, as the fetch call happens inside another function which converts the result into JSON, that then gets parsed and passed out.

 

The error description is massive, which suggests to me that I've stumbled across some problem deep in Hubspot's environment. I've cribbed most of the code from the example supplied by the API I'm trying to consume and feed into Hubspot. Is there a "correct way" to use fetch in a custom action, or do I have to abandon the API's example fetch-using code and build something myself using requests instead?

0 Avaliação positiva
4 Respostas 4
MMartin78
Membro

Using fetch() instead of requests library in custom action?

Here's what I have at this point:

 

const hubspot = require('@hubspot/api-client');

exports.main = (event, callback) => {
let endresult = processEvent(event)
console.log(endresult)
return callback(endresult);
};

function processEvent(event) {

// secrets can be accessed via environment variables

const hubspotClient = new hubspot.Client({

apiKey: process.env.HAPIKEY

});

hubspotClient.crm.contacts.basicApi.getById(event["object"]["objectId"], ["email", "phone"])

.then(results => {

 

let companyName = results.body.properties.name;

let address = results.body.properties.address;

let state = results.body.properties.state_address;

let result = {}

function prediction() {

const url = 'https://apiv2.usemarvin.ai/naics/naics/search/additional'

const data = {
"token": process.env.Relativity6_API_Key,
"name": companyName,
"address": address,
"state": state
}

fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
})
.then(resp => {
return resp.json()
})
.then(res =>
console.log(res))
.catch(err =>
console.log(err)
)
}
result = prediction()
console.log(result)


return {

outputFields: {

primary_naics_code: result.naics_prediction_a.naics_code,

primary_naics_description: result.naics_prediction_a.naics_title,

primary_naics_code_conf_score: result.naics_prediction_a.confidence_score,

primary_naics_code_last_rtrvl_date: result.response_timestamp,

primary_naics_code_update_source: "Relativity6",
secndary_naics_code_1: result.naics_prediction_b.naics_code,

secondary_naics_description_1: result.naics_prediction_b.naics_title,

secondary_naics_code_conf_score_1: result.naics_prediction_b.confidence_score,

secondary_naics_code_last_rtrvl_date_1: result.response_timestamp,

secondary_naics_code_update_source: "Relativity6"

}

}

})
.then(result =>{return result})
.catch(err => {

console.error(err)

})
}

 

I have already separately checked the API, blaming that is no good. The problem is either in the Hubspot API library or the Hubspot custom code action environment. I can't see anything from console.log, either it's not available trough the workflow's history or the junk the environment spits out clogs some 4KB limit on output text when I test the code. Anyone got an idea what's wrong, here? I'm doing my best to imitate Hubspot's example code, but if it doesn't work for anything slightly more complex than the demos...

0 Avaliação positiva
MMartin78
Membro

Using fetch() instead of requests library in custom action?

I can say that the fetch() call is handled, and successful calls' results are passed into an object using " return resp.json()", so I'm handling what is coming out. I managed to make some headway by having main consist of a lone callback(otherFunction()) call, like one of the HubSpot examples, and put all my code in otherFunction. It's gotten me past the unhandled promise exception, now Hubspot just says my outputs aren't defined in code, which is patently false, as the object passed to callback() is an object with outputFields defined correctly according to their format. 

0 Avaliação positiva
miljkovicmisa
Top colaborador(a) | Parceiro Platinum
Top colaborador(a) | Parceiro Platinum

Using fetch() instead of requests library in custom action?

Hello @MMartin78 , sometimes this error is due to not handling the promise with catch(). It is difficult to debug this without seeing your code, but if you don't already then add a chained method to your function to handle the catch(), in most cases this is the issue because promises are expected to handle rejection.

Hope I gave you some insight, for more information please share your code so I can help you further.

dennisedson
Equipe de Produto da HubSpot
Equipe de Produto da HubSpot

Using fetch() instead of requests library in custom action?

@miljkovicmisa , have you worked with custom coded actions in a workflow?

0 Avaliação positiva