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获得相同的结果时,崩溃会产生这两个错误:
(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.
关于这个问题的任何想法?
答案 0 :(得分:0)
将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)