How to check if a JavaScript object is empty
I had the following snippet of code and wanted to log an error only if whatever the catch block receives is an actual object with actual values. Needed a solution. try { const attr = await this.contract.methods.attributes(index) .call(this.defaultConfig); const identifierText = Web3.utils.hexToUtf8(attr); return identifierText; } catch (attributeNotFoundError) { logger.error(`Attribute not found`, attributeNotFoundError); return null; } The code calls a smart contract getter, using Web3.js, retrieves whatever the return value is, and returns its plain text representation.
Read more →