bigchaindb / js-bigchaindb-driver

Official BigchainDB JavaScript driver for Node.js and the browser
https://docs.bigchaindb.com/projects/js-driver/en/latest/usage.html
Apache License 2.0
216 stars 92 forks source link

typeerror: cannot read property '0' of undefined #280

Open robelkebede opened 5 years ago

robelkebede commented 5 years ago

TypeError: Cannot read property '0' of undefined chain_app_1 | at /app/node_modules/bigchaindb-driver/dist/node/transaction.js:285:49 chain_app_1 | at Array.map () chain_app_1 | at Function.makeTransferTransaction (/app/node_modules/bigchaindb-driver/dist/node/transaction.js:280:41) chain_app_1 | at Promise (/app/chain.js:205:60) chain_app_1 | at new Promise () chain_app_1 | at foodchain.transferAsset (/app/foodchain.js:198:16) chain_app_1 | at app.post (/app/index.js:52:13) chain_app_1 | at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5) chain_app_1 | at next (/app/node_modules/express/lib/router/route.js:137:13) chain_app_1 | at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3

frank-nankivell commented 5 years ago

I am experiencing the same issue, did this get resolved or is it an error with implementation?

frank-nankivell commented 5 years ago

The error comes when using the approach to create a transaction as set within the docs: http://docs.bigchaindb.com/projects/js-driver/en/latest/usage.html but are triggering an error. I am new to using bighchain so unaware if I am implementing this in error.

The error is within this part of the driver:

node_modules/bigchaindb-driver/dist/node/transaction.js:285

See code


      key: 'makeTransferTransaction',
        value: function makeTransferTransaction(unspentOutputs, outputs, metadata) {
            var inputs = unspentOutputs.map(function (unspentOutput) {
                var _tx$outputIndex = { tx: unspentOutput.tx, outputIndex: unspentOutput.output_index },
                    tx = _tx$outputIndex.tx,
                    outputIndex = _tx$outputIndex.outputIndex;

                var fulfilledOutput = tx.outputs[outputIndex];
                var transactionLink = {
                    'output_index': outputIndex,
                    'transaction_id': tx.id
                };

                return Transaction.makeInputTemplate(fulfilledOutput.public_keys, transactionLink);
            });

I am using "bigchaindb-driver": "^4.1.0",

The code I am using to call the driver and make the transaction is set out below


 module.exports.transferAsset = function(req, res) {

    const conn = new driver.Connection(API_PATH)

    // patientID_79 08102019
    // written as variable directly whilst testing
    var privateKey = 'AE1hUbzmmDe6CPsMNVM48KSrSqcFAjUF9gbDDYbhVVwJ';

    // CREATE transaction id
    // written as variable directly whilst testing
    var id = '44f30e4feb82f53ae1407aed61d43a82c3db2891fa039019d926dfeb340c1714';

    // create new owner for the asset
    var name = new driver.Ed25519Keypair()

    if(name.publicKey != null || name.publickey != undefined)
    {
        console.log('New persons public key', name.publicKey)
        // find original transaction via the transaction ID
        let transaction = conn.getTransaction(id)
        // ^^ maybe the above functio is undefined
        //console.log('transaction ID', transaction)
        if (transaction == null ||transaction == undefined) {
          var error = 'error';
          res.status(400).json(error)
          return;
        } 
        else
        {
          console.log(transaction, 'transaction') 
            // create new transfer transaction
          const newTransfer = driver.Transaction.makeTransferTransaction(
          // unspent outputs
          [{ tx: transaction, output_index: 0 }],
          // outputs
          [driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(name.publicKey))],

            // metadata
            {
            time: '30 days',
            research_project: 'analysis',
            lay_summary:'analysis on xyz'
            }

          ).then(() => {
            res.status(200).json(newTransfer)
            console.log(newTransfer)
          });
        };

      };
      var error2 = 'error';
      res.status(400).json(error2)
      return;
  };

Error message is the same as earlier ticket:

TypeError: Cannot read property '0' of undefined

frank-nankivell commented 5 years ago

Coming back here to provide input for any other user coming into the same issue. The problem is entirely due to implementation. I assume this can be closed - but not my ticket in the first place.

The reason for the error is the use of this function:

let transaction = conn.getTransaction(id)

Which needs to be implemented like this:


        conn.getTransaction(id)
        .then((result) => {
          console.log('getTransaction as callback: ', result)

          const newTransfer = driver.Transaction
            .makeTransferTransaction(
          [{ 
            tx: result, 
            output_index: 0
          }],
        // outputs
        [driver.Transaction.makeOutput(
          driver.Transaction
          .makeEd25519Condition(name.publicKey))],
          // metadata
          {
          time: '30 days',
          research_project: 'analysis',
          lay_summary:'analysis on xyz'
          }
        )