事件处理程序中的Node.js异步调用

时间:2020-09-16 16:59:05

标签: node.js events async-await

以下代码:

    async function eventQuery(){
    var contract_abi = await getContractAbi(CONTRACT_ADDRESS_ADDRESS_PROVIDER);
    const addressProvider = new web3Http.eth.Contract(contract_abi, CONTRACT_ADDRESS_ADDRESS_PROVIDER);
    const contract_address_1 = await addressProvider.methods.getAddress1().call();
    contract_abi = await getContractAbi(contract_address_1 );
    const contract = new web3Ws.eth.Contract(contract_abi, contract_address_1 );
    contract .events.allEvents()
    .on('data', async function(event) {
        if(event.raw.topics[0] === DEPOSIT) {
            var result = await axios.get('https://myurl.com/api/query');
            console.log(result.data);
        }
    })
    .on('error', (error) => {
        console.log(error);
    }); 
}

eventQuery();

有时我会出错 TypeError: Cannot read property 'data' of null

,这意味着不等待http get调用。 有谁知道如何解决这个问题?

0 个答案:

没有答案