繁体   English   中英

此 Web3.js 调用没有结果

[英]No result is happening for this Web3.js call

const fs = require('fs')
const web3 = new Web3("ws://localhost:7545");


const contract_address = "0x7484d32e8911817702c5d7c764dBF7e592000b415";


async function web3Contract() {
    const contract_abi = fs.readFileSync('./build/contracts/Bottle.json', 'utf8')
    const abi = JSON.parse(contract_abi).abi;
    // console.log(abi);
    const Bottle = await new web3.eth.Contract(abi, contract_address);
    const accounts = await web3.eth.getAccounts();
    await Bottle.methods.setName("Palm").send({from:accounts[0]});
    const greeting = await Bottle.methods.getGreeting().call();

    console.log(greeting);
    });
}

async function run() {
    try {
        await web3Contract();
    } catch (err) {
        console.log('Your error is this - ' , err);
    } finally {
        console.log('finally');
    }
}

run(); 

getGreeting().call() 给了我这个错误。 我尝试了许多不同的方法并被困在这里几个小时。 不知道该怎么做。 https://gyazo.com/81970d03c1380cd513998f25deef9e40

您错误地结合了await和回调 function (在您的情况下为function(result) {} )。

由于您使用的是await ,它永远不会到达回调 function,并且返回的Promiseawait语句解决。

在您的代码上下文中,从await返回值然后打印它更有意义。

// remove the callback function
const greeting = await Bottle.methods.getGreeting.call();
console.log(greeting);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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