简体   繁体   中英

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. But when trying to get the same result using nodejs it crashes giving those 2 errors :

(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")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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