简体   繁体   中英

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() is giving me this error. I tried numerous different ways and been stuck on here for hours. Not to sure what to do. https://gyazo.com/81970d03c1380cd513998f25deef9e40

You're incorrectly combining await and the callback function (in your case function(result) {} ).

Since you're using await , it never reaches the callback function, and the returned Promise is resolved by the await statement.

In the context of your code, it makes more sense to return the value from the await and then print it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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