1

Using Stellar's Javascript SDK

Goal

Output an array containing all the memos:
['324567567', 'anotherMemo', '239210923', '2349028509']

This array will be used to check against a database and confirm transactions.

Problem

I have isolated the memos from every transaction; although, the output is delineated by newlines. My attempts with join() and a for-loop are unsuccessful.

My Code

var StellarSdk = require('stellar-sdk');
var server = new StellarSdk.Server('https://horizon-testnet.stellar.org');

const hostAccountId = "GDUBG6QT24UYGBIAKRIVDA776STGIFM4XMHTAYGHOSCR44JIKN64JDKG" // Load hostAccountId from database ("test value")

server.transactions()
    .forAccount(hostAccountId)
    .limit(200)
    .order('desc')
    .call()
    .then(function (page) {
      page.records.forEach( function(record) {
        if (record.memo === undefined) {} else {
          var memo;
          memo = (record.memo);
          console.log(memo);
        }
});
    })
    .catch(function (err) {
        console.log(err);
    });

Output:

18446744073709551615
12345678910
Another Memo
Special Memo only
What does this memo say

Background

I am very new to Javascript and to programming on Stellar. Thank you for your time!

1 Answer 1

1

Instantiate an empty array with

let memos = [];

And add an element with the push method.

memos.push( memo );

Snippet:

  let memos = [];
  page.records.forEach( function(record) {
    if (record.memo === undefined) {} else {
      var memo;
      memo = (record.memo);
      memos.push(memo);
    }
  })
  console.log(memos);
1
  • Thank you, that works getting all the values into an array. The output shows the array building up--call by call-- until the last return is comprised of all previous iterations in one array. For example the first returned array has only memo1, then the second array has memo1, memo2 and this builds until the last array is memo1, ... , memoX. I am trying to build off of your solution to output one array with all the messages.
    – newdev
    Jun 10, 2020 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.