Search code examples
node.jstrontronweb

Nodejs fix [object Promise]


i want to get the balace of the tron wallet.
when i want to return it to show it on the web page with expressjs it print [object Promise]

var express = require('express');
var router = express.Router();
const TronWeb = require('tronweb');
const tronWeb = new TronWeb({
        fullNode: 'https://api.trongrid.io',
        solidityNode: 'https://api.trongrid.io',
        eventServer: 'https://api.trongrid.io'
    }
)
/* GET home page. */
const userBalance = async function() {
  const getUserBalance = await tronWeb.trx.getBalance("TNBE5fs2tGTnaJhCvd1Qy2BQ1k2j7Yv5zs")
  const userBalance = getUserBalance;
   return userBalance;
};
router.get('/', function(req, res, next) {
  //userBalance();
  res.render('index', {
      title: 'Express',
      userBalance: userBalance(),
  });
});

module.exports = router;

Solution

  • You should write

    router.get('/', async function(req, res, next) {
      const userBalance = await userBalance();
      res.render('index', {
          title: 'Express',
          userBalance: userBalance
      });
    });