I added node js code to gatsby node js want to do on click event

hello here is my code js to post data hubspot I would like to add onclick event to submit the data

var request = require("request");

var options = { method: 'POST',
  url: 'https://api.hubapi.com/contacts/v1/contact/',
  qs: { hapikey: 'demo' },
  headers: 
   { 
     'Content-Type': 'application/json' },
  body: 
   { properties: 
      [ { property: 'email', value: 'testingapis@hubspot.com' },
        { property: 'firstname', value: 'test' },
        { property: 'lastname', value: 'testerson' },
        { property: 'website', value: 'http://hubspot.com' },
        { property: 'company', value: 'HubSpot' },
        { property: 'phone', value: '555-122-2323' },
        { property: 'address', value: '25 First Street' },
        { property: 'city', value: 'Cambridge' },
        { property: 'state', value: 'MA' },
        { property: 'zip', value: '02139' } ] },
  json: true };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

Hi @userweb

How are you running this code? Is it in a serverless function, or client-side (browser) script?

Is there something in this that isn’t working? You can find onclick examples on MDN here. If you want a global click event perhaps this post on Stack Overflow might help.

i added it to gatsby-node.js and the post data worked fine but i would like to add on click i mean do the post when i click on button how i can do it

You will need to add an onclick (see previous link) event or use addEventListener to your button.

The onclick example gives you the basic method to add the event. Inside the function you can do whatever you want, from logging to console, to calling another function and posting data.

The same is true for the addEventListener examples.

1 Like