Getting undefined response on customer create

Hi,
I am trying to store some information on my backend when I create a user, the information is supposed to be the response from creating a customer, but I get undefined.
Here is my code.
import { json } from “express”;
//This class is used for creating customers on Dwolla.
var dwolla = require(‘dwolla-v2’);

var key = “0000”
var secret = “0000”
var client = new dwolla.Client({
// key: process.env.DWOLLA_APP_KEY,
key:key,
// secret: process.env.DWOLLA_APP_SECRET,
secret:secret,
environment: ‘sandbox’,
});

//RequestBody contains the information pertaining to the user.
var requestBody = {
firstName: firstName,
lastName: lastName,
email: email,
type: ‘personal’,
address1: address1,
city: city,
state: state,
postalCode: postalCode,
dateOfBirth: dateOfBirth,
// For the first attempt, only the
// last 4 digits of SSN required
// If the entire SSN is provided,
// it will still be accepted
ssn: ssn
};

asyncCall();
async function asyncCall() {
try {
client.auth.client()
.then(appToken => appToken.post(‘customers’, requestBody))
// .then.post(‘customers’, requestBody)
.then(res => res.headers.get(‘location’)) // => ‘https://api-sandbox.dwolla.com/customers/fc451a7a-ae30-4404-aB95-e3553fcd733f
// .then(res => console.log(res.headers.get(‘location’)))
.then(async function(res) {
const result = await (JSON.stringify(res.body));
const result2 = await (JSON.stringify(res.headers));

       console.log(result)
       console.log(result2)
      });
   
  } catch (err) {
    console.log(err)
    console.log("Did not work");
   

  }



// expected output: 'resolved'

}

Hi @adoba - it looks like it may be due to promises chaining that you’re getting undefined on the second .then().

You could do something like the following for accessing the headers/body/status:

dwolla
  .post("customers", {
    firstName: "Jane",
    lastName: "Doe",
    email: "jane@doe.com"
  })
  .then(
    function(res) {
        const location = (JSON.stringify(res.headers.get("location")));
        // res.status  => 201
        // res.body     => Object or String depending on response type
    },
    function(error) {
        // when the server return a status >= 400
        // error.status   => 400 
        // error.headers  => Headers { ... }
        // error.body     => Object or String depending on response type
    }
  );

It also looks like you may be using an older version of our Node SDK. I’d recommend checking out the latest version which comes with niceties, likeToken Management, baked in.

Hope that helps!

1 Like

Definitely did, awesome thanks!

1 Like