简体   繁体   English

Node.js UnhandledPromiseRejection:TypeError:无法读取未定义的属性“ sign”

[英]Node.js UnhandledPromiseRejection: TypeError: Cannot read property 'sign' of undefined

tran.js tran.js

var CoinStack = require('coinstack-sdk-js')

var coinstackclient = new CoinStack('YOUR_COINSTACK_ACCESS_KEY',
'YOUR_COINSTACK_SECRET_KEY'); // Actual keys not shown

var privateKeyWIF = CoinStack.ECKey.createKey(); //개인키 생성

var txBuilder = coinstackclient.createTransactionBuilder();
txBuilder.addOutput("1Q8xE8T3G9mxRoDUde6gDSxnK1uCac2kqh", 
CoinStack.Math.toSatoshi("0.01"))
txBuilder.setInput("1Q8xE8T3G9mxRoDUde6gDSxnK1uCac2kqh");
  txBuilder.buildTransaction(function(err, tx) {
  tx.sign(privateKeyWIF)
  var rawTx = tx.serialize()
  // send tx
  client.sendTransaction(rawTx, function(err) {
    if (null != err) {
        console.log("failed to send tx");
      }
   });
});

Error 错误

(node:12012) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'sign' of undefined (node:12012) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. (节点:12012)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):TypeError:无法读取未定义的属性“ sign”(节点:12012)[DEP0018] DeprecationWarning:不处理未处理的承诺拒绝。 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。

I don't know what to do 我不知道该怎么办

回调的第二个参数'tx'很可能是未定义的。

检查tx是否不为nullundefined ,然后为ex调用其函数,例如if (ex != null && ex.sign) { ... }

Most likely txBuilder.buildTransaction() fails, and indeed your tx parameter in the callback function is not the expected object. 最有可能的txBuilder.buildTransaction()失败,并且实际上回调函数中的tx参数不是预期的对象。

You should check for errors, before using tx , like so: 您应该在使用tx之前检查错误,如下所示:

txBuilder.buildTransaction(function(err, tx) {
    if (err) {
        // output err or do something with it
        console.log('failed to build transaction');
        console.log(err);

        // stop here.
        return;
    }


    tx.sign(privateKeyWIF)
    var rawTx = tx.serialize();
        // send tx
        client.sendTransaction(rawTx, function(err) {
        if (err) {
            console.log("failed to send tx");
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM