简体   繁体   English

web3、松露、nodejs 错误:UnhandledPromiseRejectionWarning

[英]web3, truffle, nodejs error : UnhandledPromiseRejectionWarning

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);
});

I set this code to call a smart contract function that returns 5. I tested it with truffle console and it works properly.我将此代码设置为调用返回 5 的智能合约函数。我使用 truffle 控制台对其进行了测试,并且它工作正常。 But when trying to get the same result using nodejs it crashes giving those 2 errors :但是当尝试使用 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.

Any idea about the issue ?对这个问题有什么想法吗?

Replace MyContract definition with const MyContract = artifacts.require("MyContractewCoin")const MyContract = artifacts.require("MyContractewCoin")替换 MyContract 定义

// 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)

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

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