<commit_before>'use strict';

/**
 * Module dependencies.
 */
var Transaction = require('../models/Transaction');
var Block       = require('../models/Block');
var Address     = require('../models/Address');
var async       = require('async');
var common      = require('./common');


/**
 * Find transaction by hash ...
 */
exports.transaction = function(req, res, next, txid) {
  Transaction.fromIdWithInfo(txid, function(err, tx) {
    if (err || ! tx)
      return common.handleErrors(err, res);
    else {
      req.transaction = tx.info;
      return next();
    }
  });
};


/**
 * Show transaction
 */
exports.show = function(req, res) {

  if (req.transaction) {
    res.jsonp(req.transaction);
  }
};


var getTransaction = function(txid, cb) {
  Transaction.fromIdWithInfo(txid, function(err, tx) {
    if (err) {
      console.log(err);
      return cb(err);
    }
    return cb(null, tx.info);
  });
};


/**
 * List of transaction
 */
exports.list = function(req, res, next) {
  var limit = req.query.limit || 5;
  var bId = req.query.block;
  var addrStr = req.query.address;
  var page = req.query.pageNum;
  var pageLength = 20;
  var pagesTotal = 1;
  var txLength;
  var txs;

  if (bId) {
    Block.fromHashWithInfo(bId, function(err, block) {
      if (err && !block) {
        console.log(err);
        res.status(404).send('Not found');
        return next();
      }

      txLength = block.info.tx.length;

      if (page) {
        var spliceInit = page * pageLength;
        txs = block.info.tx.splice(spliceInit, pageLength);
        pagesTotal = Math.ceil(txLength / pageLength);
      }
      else {
        txs = block.info.tx;
      }

      async.mapSeries(txs, getTransaction,
        function(err, results) {
          res.jsonp({
            pagesTotal: pagesTotal,
            txs: results
          });
        });
    });
  }
  else if (addrStr) {
    var a = Address.new(addrStr);

    a.update(function(err) {
      if (err && !a.totalReceivedSat) {
        console.log(err);
        res.status(404).send('Invalid address');
        return next();
      }

      txLength = a.transactions.length;

      if (page) {
        var spliceInit = page * pageLength;
        txs = a.transactions.splice(spliceInit, pageLength);
        pagesTotal = Math.ceil(txLength / pageLength);
      }
      else {
        txs = a.transactions;
      }

      async.mapSeries(txs, getTransaction,
        function(err, results) {
          res.jsonp({
            pagesTotal: pagesTotal,
            txs: results
          });
        });
    });
  }
  else {
    Transaction
      .find()
      .limit(limit)
      .sort('-time')
      .exec(function(err, txs) {
        if (err) {
          res.status(500).send(err);
        } else {
          var txids = [];
          for(var i=0;i<txs.length;i++) {
            txids.push(txs[i].txid);
          }

          async.mapSeries(txids, getTransaction, function(err, alltxs) {
            res.jsonp({
              txs: alltxs,
              length: alltxs.length
            });
          });
        }
      });
  }

};
<commit_msg>fix error when there are not transaccion<commit_after>'use strict';

/**
 * Module dependencies.
 */
var Transaction = require('../models/Transaction');
var Block       = require('../models/Block');
var Address     = require('../models/Address');
var async       = require('async');
var common      = require('./common');


/**
 * Find transaction by hash ...
 */
exports.transaction = function(req, res, next, txid) {
  Transaction.fromIdWithInfo(txid, function(err, tx) {
    if (err || ! tx)
      return common.handleErrors(err, res);
    else {
      req.transaction = tx.info;
      return next();
    }
  });
};


/**
 * Show transaction
 */
exports.show = function(req, res) {

  if (req.transaction) {
    res.jsonp(req.transaction);
  }
};


var getTransaction = function(txid, cb) {
  Transaction.fromIdWithInfo(txid, function(err, tx) {
    if (err) {
      console.log(err);
      return cb(err);
    }

    if (!tx) return cb(new Error('Transaction not found'));

    return cb(null, tx.info);
  });
};


/**
 * List of transaction
 */
exports.list = function(req, res, next) {
  var limit = req.query.limit || 5;
  var bId = req.query.block;
  var addrStr = req.query.address;
  var page = req.query.pageNum;
  var pageLength = 20;
  var pagesTotal = 1;
  var txLength;
  var txs;

  if (bId) {
    Block.fromHashWithInfo(bId, function(err, block) {
      if (err && !block) {
        console.log(err);
        res.status(404).send('Not found');
        return next();
      }

      txLength = block.info.tx.length;

      if (page) {
        var spliceInit = page * pageLength;
        txs = block.info.tx.splice(spliceInit, pageLength);
        pagesTotal = Math.ceil(txLength / pageLength);
      }
      else {
        txs = block.info.tx;
      }

      async.mapSeries(txs, getTransaction, function(err, results) {
        if (err) {
          console.log(err);
          res.status(404).send('TX not found');
        }

        res.jsonp({
          pagesTotal: pagesTotal,
          txs: results
        });
      });
    });
  }
  else if (addrStr) {
    var a = Address.new(addrStr);

    a.update(function(err) {
      if (err && !a.totalReceivedSat) {
        console.log(err);
        res.status(404).send('Invalid address');
        return next();
      }

      txLength = a.transactions.length;

      if (page) {
        var spliceInit = page * pageLength;
        txs = a.transactions.splice(spliceInit, pageLength);
        pagesTotal = Math.ceil(txLength / pageLength);
      }
      else {
        txs = a.transactions;
      }

      async.mapSeries(txs, getTransaction, function(err, results) {
        if (err) {
          console.log(err);
          res.status(404).send('TX not found');
        }

        res.jsonp({
          pagesTotal: pagesTotal,
          txs: results
        });
      });
    });
  }
  else {
    Transaction
      .find()
      .limit(limit)
      .sort('-time')
      .exec(function(err, txs) {
        if (err) {
          res.status(500).send(err);
        } else {
          var txids = [];
          for(var i=0;i<txs.length;i++) {
            txids.push(txs[i].txid);
          }

          async.mapSeries(txids, getTransaction, function(err, alltxs) {
            if (err) {
              console.log(err);
              res.status(404).send('TX not found');
            }

            res.jsonp({
              txs: alltxs,
              length: alltxs.length
            });
          });
        }
      });
  }

};

{'fraction_non_alphanumeric': 0.14826593972436772, 'fraction_numerical': 0.005603513554444949, 'mean_word_length': 1.8763066202090593, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4016381', 'n_tokens_mistral': 2160, 'n_tokens_neox': 2094, 'n_words': 431}