question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'baseUrl' of undefined

See original GitHub issue

If I try to run that piece of code:

var gateway = braintree.connect({
  environment: braintree.Environment[config.braintree.environment],
  merchantId: config.braintree.merchantId,
  publicKey: config.braintree.publicKey,
  privateKey: config.braintree.privateKey
});

module.exports.gateway = gateway;

I got a TypeError:

TypeError: Cannot read property 'baseUrl' of undefined
    at Config.baseUrl (***/node_modules/braintree/lib/braintree/config.js:36:28)
    at Config.baseMerchantUrl (***/node_modules/braintree/lib/braintree/config.js:40:17)
    at new TransparentRedirectGateway (***/node_modules/braintree/lib/braintree/transparent_redirect_gateway.js:39:34)
    at new BraintreeGateway (***/node_modules/braintree/lib/braintree/braintree_gateway.js:69:32)
    at Object.connect (***/node_modules/braintree/lib/braintree.js:42:10)
    at Object.<anonymous> (***/helper/braintree.js:10:25)

Any ideas on that?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5

github_iconTop GitHub Comments

4reactions
sidonaldsoncommented, Nov 13, 2017

To anyone else who came across this issue. I had to change the environment property to load from braintree.Environment.Sandbox rather than passing in "Sandbox"

braintree.connect({
    environment: braintree.Environment.Sandbox,
    merchantId: process.env.BT_MERCHANT_ID,
    publicKey: process.env.BT_PUBLIC_KEY,
    privateKey: process.env.BT_PRIVATE_KEY
});

or you could amend to the following and still pass "Sandbox"

braintree.connect({
    environment: braintree.Environment[process.env.BT_ENVIRONMENT],
    merchantId: process.env.BT_MERCHANT_ID,
    publicKey: process.env.BT_PUBLIC_KEY,
    privateKey: process.env.BT_PRIVATE_KEY
});
0reactions
moshebeericommented, Jan 3, 2018

This worked for me


router.get('/checkouts/new', controller.checkouts_new);
router.get('/checkouts/:id', controller.checkouts_id);
router.post('/checkouts', controller.checkouts);


////////////////////////////////////////////////
//// see: https://github.com/braintree/braintree_express_example/blob/master/routes/index.js

let braintree = require('braintree');
let gateway;

//require('dotenv').load(); process.env.BT_MERCHANT_ID;
//environment = process.env.BT_ENVIRONMENT.charAt(0).toUpperCase() + process.env.BT_ENVIRONMENT.slice(1);
gateway = braintree.connect({
  environment: braintree.Environment.Sandbox, //braintree.Environment['Sandbox']
  merchantId: 'XXXX',
  publicKey: 'YYYY',
  privateKey: 'ZZZZ'
});

let TRANSACTION_SUCCESS_STATUSES = [
  braintree.Transaction.Status.Authorizing,
  braintree.Transaction.Status.Authorized,
  braintree.Transaction.Status.Settled,
  braintree.Transaction.Status.Settling,
  braintree.Transaction.Status.SettlementConfirmed,
  braintree.Transaction.Status.SettlementPending,
  braintree.Transaction.Status.SubmittedForSettlement
];


function formatErrors(errors) {
  let formattedErrors = '';

  for (let i in errors) { // eslint-disable-line no-inner-declarations, vars-on-top
    if (errors.hasOwnProperty(i)) {
      formattedErrors += 'Error: ' + errors[i].code + ': ' + errors[i].message + '\n';
    }
  }
  return formattedErrors;
}

function createResultObject(transaction) {
  let result;
  let status = transaction.status;

  if (TRANSACTION_SUCCESS_STATUSES.indexOf(status) !== -1) {
    result = {
      header: 'Sweet Success!',
      icon: 'success',
      message: 'Your test transaction has been successfully processed. See the Braintree API response and try again.'
    };
  } else {
    result = {
      header: 'Transaction Failed',
      icon: 'fail',
      message: 'Your test transaction has a status of ' + status + '. See the Braintree API response and try again.'
    };
  }

  return result;
}

// GET /checkouts/new
exports.checkouts_new = function (req, res) {
  gateway.clientToken.generate({}, function (err, response) {
    if(err) return handleError(res, err);
    return res.status(200).json({clientToken: response.clientToken});
  });
};

// GET /checkouts/:id
exports.checkouts_id = function (req, res) {
  let result;
  let transactionId = req.params.id;

  gateway.transaction.find(transactionId, function (err, transaction) {
    if(err) return handleError(res, err);
    result = createResultObject(transaction);
    return res.status(200).json({transaction: transaction, result: result});

  });
};

// POST /checkouts
exports.checkouts = function (req, res) {
  let transactionErrors;
  let amount = req.body.amount; // In production you should not take amounts directly from clients
  let nonce = req.body.payment_method_nonce;

  gateway.transaction.sale({
    amount: amount,
    paymentMethodNonce: nonce,
    options: {
      submitForSettlement: true
    }
  }, function (err, result) {
    if (result.success || result.transaction) {
      res.redirect('' + result.transaction.id);
    } else {
      transactionErrors = result.errors.deepErrors();
      //req.flash('error', {msg: formatErrors(transactionErrors)});
      res.redirect('new');
    }
  });
};
//////////////////////////
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot set property 'baseUrl' of undefined - Stack Overflow
The error in cause is: TypeError: Cannot set property 'baseUrl' of undefined. var AuthClient = (function () { function AuthClient($http, baseUrl) ...
Read more >
TypeError: Cannot read property 'baseUrl' of undefined · Issue ...
Describe the bug I used npx -p @storybook/cli sb init to add Storybook to my Angular component library monorepo (using Lerna).
Read more >
Cannot read property 'baseUrl' of undefined - Bountysource
angular.js:14642 TypeError: Cannot read property 'baseUrl' of undefined at new controller (formio.js:15713)
Read more >
Cannot read property 'baseUrl' of undefined-Reactjs
Coding example for the question React Jest TypeError: Cannot read property 'baseUrl' of undefined-Reactjs.
Read more >
cannot set properties of undefined (setting 'axios') - You.com
Axios Uncaught TypeError: Cannot read property 'map' of undefined. 1. Clicking routes in Header refreshes page in vuejs with prerender-spa plugin after ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found