将结果值传输到新选项卡

时间:2017-07-24 03:36:24

标签: javascript bitcore

我使用BitcoreJavaScript的比特币应用程序,并使用下面提供的功能获取控制台日志 -

   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);
                });
            };

下面提供的控制台打印

{
      "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
        }
      ]
    }

对于console.log(result.transactions[i].amount);,我得到undefined printpout。当我尝试使用amount.toString()时,我收到错误,因为它试图获得toString()的{​​{1}}。

enter image description here

当我在undefined之后使用代码window.open('transactions.html?result='+ result);时,我可以打开一个setConsoleData值为

的新标签页

URL

我需要在新file:///Users/Myelan/Documents/Projects/Wallet-App-JS/transactions.html?result=[object%20Object]标签中显示所有与addresses, the amount and the txid的交易,我需要知道如何将HTML值传递给新标签。

我怎么能实现这个目标?

result

当我编写代码Note 时,我在控制台中得到以下内容,

enter image description here

1 个答案:

答案 0 :(得分:1)

使用result = JSON.parse(result);将json响应字符串解析为JSON对象。

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);
    });
};