Javascript Node.js-以太坊合约无法调用函数
我是区块链开发新手。为了了解目的,开始在Node.js中使用以太坊区块链概念开发小型合同 我已安装了包Javascript Node.js-以太坊合约无法调用函数,javascript,node.js,blockchain,ethereum,web3,Javascript,Node.js,Blockchain,Ethereum,Web3,我是区块链开发新手。为了了解目的,开始在Node.js中使用以太坊区块链概念开发小型合同 我已安装了包“solc”:“^0.4.24”和“web3”:“^0.20.7”,以在节点中生成和编译我的合同 My Grades.sol文件: pragma solidity ^0.4.24; contract Grades { mapping (bytes32 => string) public grades; bytes32[] public studentList;
“solc”:“^0.4.24”
和“web3”:“^0.20.7”
,以在节点中生成和编译我的合同
My Grades.sol文件:
pragma solidity ^0.4.24;
contract Grades {
mapping (bytes32 => string) public grades;
bytes32[] public studentList;
function Grades(bytes32[] studentNames) public {
studentList = studentNames;
}
function giveGradeToStudent(bytes32 student, string grade) public {
require(validStudent(student));
grades[student] = grade;
}
function validStudent(bytes32 student) view public returns (bool) {
for(uint i = 0; i < studentList.length; i++) {
if (studentList[i] == student) {
return true;
}
}
return false;
}
function getGradeForStudent(bytes32 student) view public returns (string) {
require(validStudent(student));
return grades[student];
}
}
我试图编译并运行我得到了异常
TypeError: deployedContract.giveGradeToStudent is not a function
在deployedContract中,我可以看到这些方法。有人能帮我吗
注意:我已安装了
“ganache cli”:“^6.1.8”
,用于添加事务并单独运行。我能够在ganache中看到交易。我认为问题在于:
deployedContract = GradesContract.new(['John','James'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000});
.new()
此处不会返回已部署的契约实例。(我不确定它是否会返回任何内容?)您需要回调,如下所示:
deployedContract = GradesContract.new(
['John', 'James'],
{data: byteCode, from: web3.eth.accounts[0], gas: 4700000},
function (err, instance) {
if (instance.address) {
instance.giveGradeToStudent(...);
}
}
);
deployedContract = GradesContract.new(
['John', 'James'],
{data: byteCode, from: web3.eth.accounts[0], gas: 4700000},
function (err, instance) {
if (instance.address) {
instance.giveGradeToStudent(...);
}
}
);