1
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 的智能合约函数。我使用 truffle 控制台对其进行了测试,它可以正常工作。但是当尝试使用 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.

关于这个问题的任何想法?

4

1 回答 1

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)
于 2020-05-07T08:53:31.613 回答