js-stellar-sdk: Docs unclear, error when creating an account

I got an error from the very beginning. The code snippets in your “Create an Account” is very unclear.

Are they supposed to be inside a single or a separate files? (i pasted everything into one file) Are they run from the browser or via Terminal? (i run it using Node from Terminal)

Anyway, here’s the response that i get:

Unhandled rejection NotFoundError: [object Object]

SUCCESS! You have a new account :)
 { _links:
   { transaction:
      { href: 'https://horizon-testnet.stellar.org/transactions/4e3212bd9b1624fe501a0adc37126aafb785888fd45de3a52eb38cdbba1603d4' } },
  hash: '4e3212bd9b1624fe501a0adc37126aafb785888fd45de3a52eb38cdbba1603d4',
  ledger: 6182957,
  envelope_xdr: 'AAAAAGXNhLrhGtltTwCpmqlarh7s1DB2hIkbP//jgzn4Fos/AAAAZAAACT0AAMVDAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAD21ktzSjDM/q/1kSQANbZyD7gQDKEA0aSgM90zbk/uwAAAAXSHboAAAAAAAAAAAB+BaLPwAAAEBM2wZbgLDXe7NdKTGvHHiRGhnNsQ5F2d7TJK5scwTo69OMGWxR0K6K2kXnmWgRxLgMLaLLRRnxzIlxcT1907wO',
  result_xdr: 'AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAA=',
  result_meta_xdr: 'AAAAAAAAAAEAAAADAAAAAABeWC0AAAAAAAAAAA9tZLc0owzP6v9ZEkADW2cg+4EAyhANGkoDPdM25P7sAAAAF0h26AAAXlgtAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAwBeWC0AAAAAAAAAAGXNhLrhGtltTwCpmqlarh7s1DB2hIkbP//jgzn4Fos/AAJ6s4+6ejQAAAk9AADFQwAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAQBeWC0AAAAAAAAAAGXNhLrhGtltTwCpmqlarh7s1DB2hIkbP//jgzn4Fos/AAJ6nEdDkjQAAAk9AADFQwAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAA' }

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (3 by maintainers)

Commits related to this issue

Most upvoted comments

@rizkysyazuli - Agree, the docs in the page aren’t super clear. You can put all of them into a single file like you have seem to done. There is one caveat though.

The second and third snippets are both asynchronous. The second snippet makes an API call to fund an account (and activate it), and only after that is successful, should you be executing the third snippet to get the account details.

In your case, since you’ve pasted all the snippets as they are, in order, your third snippet is executed even before the api call in the second snippet returns. So, it is not able to find the account details you’re looking for and that fails with a NotFoundError. Since this is also a promise, and it doesn’t have the catch block you see the error Unhandled rejection NotFoundError: [object Object].

This is the correct way to use the snippets from the documentation:

// create a completely new and unique pair of keys
// see more about KeyPair objects: https://stellar.github.io/js-stellar-sdk/Keypair.html
var StellarSdk = require('stellar-sdk')
var pair = StellarSdk.Keypair.random();
pair.secret();
// SAV76USXIJOBMEQXPANUOQM6F5LIOTLPDIDVRJBFFE2MDJXG24TAPUU7
pair.publicKey();
// GCFXHS4GXL6BVUCXBWXGTITROWLVYXQKQLF4YH5O5JT3YZXCYPAFBJZB

var request = require('request');
request.get({
  url: 'https://horizon-testnet.stellar.org/friendbot',
  qs: { addr: pair.publicKey() },
  json: true
}, function (error, response, body) {
  if (error || response.statusCode !== 200) {
    console.error('ERROR!', error || body);
  }
  else {
    console.log('SUCCESS! You have a new account :)\n', body);

    var server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
    // the JS SDK uses promises for most actions, such as retrieving an account
    server.loadAccount(pair.publicKey()).then(function (account) {
      console.log('Balances for account: ' + pair.publicKey());
      account.balances.forEach(function (balance) {
        console.log('Type:', balance.asset_type, ', Balance:', balance.balance);
      });
    }).catch(function (err) {
      console.error(err);
    });
  }
});

Is that a friendbot for testnet?

I fixed friendbot URL in https://github.com/stellar/docs/commit/bdb8ea791ce060522b40c05301dfc1377bee8773 (changes will be visible at stellar.org/developers in no more than 30 minutes from now).