Transferring the result values to the new tab

44 Views Asked by At

I work with a Bitcoin app with Bitcore and JavaScript and get Console log using the function provided below-

   function getTransactions() {

                var result = {};
                var address = $('#address').find(":selected").text();
                var explorers = require('bitcore-explorers');
                var client = new explorers.Insight('testnet');

                client.getUnspentUtxos(address, function (err, transactions) {
                    result.transactions = transactions;

                    var len = result.transactions.length;

                    for (i = 0; i < len; i++) { 

                        console.log(result.transactions[i].address.toString());
                        console.log(result.transactions[i].amount);
                    }

                    setConsoleData('', result);
                });
            };

The console prints provided below,

{
      "transactions": [
        {
          "address": "mtfXS7TGSDVMyxhby4YwvACqACLwxdthxQ",
          "txid": "5d1d86257096902f53762aaf2b7d43c44bf1997523dc8878d1157349dda5846f",
          "vout": 1,
          "scriptPubKey": "76a9149038a0390fea805ca27f700f14d329a4fb7dbf4788ac",
          "amount": 0.001
        },
        {
          "address": "mtfXS7TGSDVMyxhby4YwvACqACLwxdthxQ",
          "txid": "c3f6d0129eea1dc89fcb35e1c36d3537ebf291a887f31c75b590b2ebe7d8ba1c",
          "vout": 1,
          "scriptPubKey": "76a9149038a0390fea805ca27f700f14d329a4fb7dbf4788ac",
          "amount": 0.001
        },
        {
          "address": "mtfXS7TGSDVMyxhby4YwvACqACLwxdthxQ",
          "txid": "b04229c1052e962ec2fc2cc1b924c1cd67c30864c45e314d7dc8ef45f614e7ec",
          "vout": 1,
          "scriptPubKey": "76a9149038a0390fea805ca27f700f14d329a4fb7dbf4788ac",
          "amount": 0.001
        },
        {
          "address": "mtfXS7TGSDVMyxhby4YwvACqACLwxdthxQ",
          "txid": "d5190362895dc4eb37e7f7ba68889f402771ce3fa46704a027101d94c7ab87d5",
          "vout": 1,
          "scriptPubKey": "76a9149038a0390fea805ca27f700f14d329a4fb7dbf4788ac",
          "amount": 0.001
        }
      ]
    }

For the console.log(result.transactions[i].amount);, I get undefined printpout. When I try to use amount.toString(), I get error as it was trying to get toString() of the undefined.

enter image description here

When I use the code window.open('transactions.html?result='+ result); after setConsoleData, I can open a new tab with the URL value of

file:///Users/Myelan/Documents/Projects/Wallet-App-JS/transactions.html?result=[object%20Object].

I need to have all the transactions with their addresses, the amount and the txid in the new HTML tab and I need to know how can I pass the result value to the new tab.

How could I achieve that?

Note

When I write the code result = JSON.parse(result);, I get the following print in the console,

enter image description here

1

There are 1 best solutions below

4
On BEST ANSWER

Use result = JSON.parse(result); to parse the json response string to JSON object.

function getTransactions() {
    var result = {};
    var address = $('#address').find(":selected").text();
    var explorers = require('bitcore-explorers');
    var client = new explorers.Insight('testnet');

    client.getUnspentUtxos(address, function (err, transactions) {
        result.transactions = JSON.parse(transactions);
        var len = result.transactions.length;

        for (i = 0; i < len; i++) { 

            console.log(result.transactions[i].address.toString());
            console.log(result.transactions[i].amount);
        }

        setConsoleData('', result);
    });
};