Friday, 21 April 2017

How to Populate JSON when Referencing a Node Module

I'm trying to pass variables into a function that is from a Node module, but am getting errors in the response object. Is this possible? Do I need to parse the JSON?

I tried the following:

app.js

var customer = require('./customer');
customer.createCustomer({
  given_name: "first name",
  family_name: "last name",
  email_address: "abc@email.com"
});

customer.js

const unirest = require('unirest'),
  access_token = 'sq0atp-1234',
  domain = 'https://connect.squareup.com/v2/';

module.exports = {

  createCustomer: function(given_name, family_name, email_address) {
    unirest.post(domain + 'customers')
      .headers({
        'Content-Type': 'application/json',
        "Authorization": "Bearer " + access_token
      })
      .send({
        "given_name": given_name,
        "family_name": family_name,
        "email_address": email_address
      })
      .end(function (response) {
        console.log(response);
    });
  }
}

The createCustomer function works when it's run directly using strings.



via KVNA

No comments:

Post a Comment