牢固地编写了这样的功能
function AddData(uint _index, string _projectName, string _devAddress, string _developer) public {
Datas[_index] = Data(_index, 0, _projectName, _devAddress, _developer);
}
请注意,这只是整个实体代码的一部分。在js中,我正在尝试将变量传递到函数中,但似乎不起作用。我认为这是js变量无法传递到实体字符串变量中。
counterDB.AddData(id_db, projectName, devAddress, developer, function (err, result) {
if (err) {
console.log('Error: ' + err);
}
else {
console.log(result);
}
});
传入的变量是我从数据库中提取出来的数据,以传递到智能合约中。我检查了每个数据是否都已正确插入,但无法将数据传递给函数。我是否缺少将var转换为字符串的函数?
答案 0 :(得分:0)
您是否正在使用web3 v1.0?您是否尝试过传递如下功能:
var yourContract = new web3.eth.Contract(ABI, contractAddress);
const contractFunction = yourContract.methods.AddData(id_db, projectName, devAddress, developer);
const functionBytes = contractFunction.encodeABI();
const rawTx = {
gasLimit: web3.utils.toHex(200000),
to: contractAddress,
from: addressFrom,
data: functionBytes
};
web3.eth.accounts.signTransaction(rawTx, privateKey)
.then(RLPencodedTx => {
web3.eth.sendSignedTransaction(RLPencodedTx['rawTransaction'])
.on('error', error => { callback(null, error) })
.on('receipt', receipt => { callback(receipt) });
})
请记住,如果使用私钥的json导出,则必须解密私钥:
var path = process.cwd();
const exportedAccountString = fs.readFileSync(path + '/your_key_file.json').toString();
const decrypted = web3.eth.accounts.decrypt(exportedAccountString, 'YourPasswordHere...');
const privateKey = decrypted.privateKey;