无法读取未定义的属性“数量”

时间:2018-08-11 17:47:59

标签: javascript json ajax

我正在尝试搜索API以获取每个特定地址的结果。我面临的问题是它返回错误“无法读取未定义的属性'amounts'”。我试图通过if语句来阻止它,但是它仍然会在API中失效,直到找不到结果和错误。

Fiddle

var _miner = 't1PFzdktgbdtmApkCFFkg8dCPvfXrJ2DwPm'; 
var _coin = 'xxx';
var explorerTX = 'asdf';

//Payment history
$.ajax({
  url: "https://xxxxxx.net/api/payments",
  dataType: 'json',
  success: function(data) {

    var addr = _miner;

    //Payment History List
    for (var i = 0; i < data.length; i++) {

      var payments = data[i].payments;
      for (var j = 0; j < 200; j++) {

        //console.log(payments[j].amounts[addr]);
        if (payments[j].amounts[addr]) {

          //Time
          //console.log(payments[j].time);
          var date = payments[j].time;
          var date = new Date(parseInt(date));
          var date = date.toGMTString();

          //blocks
          var block = [];
          var blocks = payments[j].blocks;
          for (var b = 0; b < blocks.length; b++) {
            //console.log(blocks[b])
            block.push(blocks[b]);
          }

          //txid
          var txid = payments[j].txid;

          //amount
          var amount = payments[j].amounts[addr].toFixed(8);

        }
      }
    }

  },
  error: function() {
    //alert("Was unable to get info!");
  }
});

2 个答案:

答案 0 :(得分:0)

错误是说上面带有amounts的对象是undefined。您的情况应该是payments[j]。在循环内console.log payments[j]的值会很有帮助。

答案 1 :(得分:0)

您可以用它来检查

(addr in payments[j].amounts)