代码之家  ›  专栏  ›  技术社区  ›  sunwarr10r

nodejs express:在console.log和callback中获取不同的输出

  •  0
  • sunwarr10r  · 技术社区  · 8 年前

    我有一个 smart contract 部署于 Kovan ,其中包含getter函数:

    function getOrderStatus(uint _orderId) public view returns(bool shipped, bool arrived, bool payed) {
        return (orders[_orderId].shipped, orders[_orderId].arrived, orders[_orderId].payed); 
    }
    

    如果我通过Web3调用函数,我会在控制台上得到以下输出,这很好!:

    Result {
      '0': false,
      '1': false,
      '2': false,
      shipped: false,
      arrived: false,
      payed: false }
    

    但如果我试图通过 callback function 为了提供它,例如通过API,我在浏览器上得到了以下输出:

    [object Object]
    

    唯一的区别是 console.log(returnValue) ---GT; callback(returnValue) 在以下代码末尾:

    function getOrderStatus(_orderId, callback) {
        contractABI.methods.getOrderStatus(_orderId).call()
            .then(returnValue => callback(returnValue));
    }
    

    然后通过调用函数 Express

    app.get('/api/getOrderStatus', function(req, res) {    
        orderId = req.query.orderId;        
        getOrderStatus(orderId, function(error, data) {
            if (!error) {
                res.send(data);
            }
            else 
            {
                res.send(error);    
            }
        });    
    });
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Sebastian Hildebrandt    8 年前

    getOrderStatus()

    function getOrderStatus(_orderId, callback) {
        contractABI.methods.getOrderStatus(_orderId).call()
            .then(returnValue => callback(returnValue));
    }
    

    returnValue callback

    error

    app.get('/api/getOrderStatus', function(req, res) {    
        orderId = req.query.orderId;        
        getOrderStatus(orderId, function(data, error) {  // <--- changed order of parameters
            if (!error) {
                res.json(data);
            } else {
                res.send(error);    
            }
        });    
    });
    

    推荐文章