【问题标题】:web3, truffle, nodejs error : UnhandledPromiseRejectionWarningweb3,松露,nodejs错误:UnhandledPromiseRejectionWarning
【发布时间】:2018-03-17 04:37:39
【问题描述】:
var     web3            = require('web3'),
    contract        = require('truffle-contract'),
    path            = require('path'),
    MyContractJSON  = require(path.join(__dirname, '../tru_dir/build/contracts/NewCoin.json'));
var     provider        = new web3.providers.HttpProvider("http://localhost:8545");

var     MyContract      = contract(MyContractJSON);

MyContract.setProvider(provider);
MyContract.deployed().then(function(instance){
return instance.returnfive();
})

.then(function(result) {
console.log(result);
}, function(error) {
console.log(error);
});

我将此代码设置为调用返回 5 的智能合约函数。 我用松露控制台测试了它,它工作正常。 但是当尝试使用 nodejs 获得相同的结果时,它会崩溃并给出这 2 个错误:

(node:6227) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property 'apply' of undefined
(node:6227) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

对这个问题有任何想法吗?

【问题讨论】:

    标签: node.js blockchain smartcontracts truffle web3js


    【解决方案1】:

    将 MyContract 定义替换为 const MyContract = artifacts.require("MyContractewCoin")

    // You are missing this step before invoking deployer
    await deployer.deploy(MyContract) 
    
    const dMyContract = await MyContract.deployed() 
    // now you can do stuff like 
    let result = await dMyContract.someContractFunction(args)
    

    【讨论】:

      猜你喜欢
      • 2018-11-15
      • 1970-01-01
      • 2018-12-16
      • 2021-06-29
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      相关资源
      最近更新 更多