Nodejs Express: Getting different output in console.log and callback

0 votes

I have a smart contract deployed on Kovan, which contains a getter function:

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

If I call the function via web3 I get the following output on the console:

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

But if I try to forward the returned object via a callback function to provide it e.g. via an API, I get the following output on my Browser:

[object Object]

The only difference is instead of console.log(returnValue) --> callback(returnValue) at the end of the following code:

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

The function is then being called via 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);    
        }
    });    
});
Oct 9, 2018 in Blockchain by slayer
• 29,050 points
26 views

1 answer to this question.

0 votes

If your getOrderStatus() function is like this:

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

... then your result is in returnValue, right? If you then call your callback function like described above, then the first parameter is your data.

In your route, you are calling the callback with the error parameter at first parameter, so I guess it should be this way:

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);    
        }
    });    
});

Hope that helps..

answered Oct 9, 2018 by Omkar
• 67,480 points

Related Questions In Blockchain

+1 vote
1 answer
+1 vote
1 answer

What is the difference between if() and require() statement in solidity??

If() and require() have separate functions and ...READ MORE

answered Apr 18, 2018 in Blockchain by Shashank
• 10,350 points
377 views
+1 vote
1 answer

How is it possible to achieve privacy and confidentiality in smart contracts??

Any contract code written on the blockchain ...READ MORE

answered Apr 18, 2018 in Blockchain by Shashank
• 10,350 points

edited Aug 7, 2018 by Omkar 104 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
140 views
0 votes
1 answer
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 67,480 points
77 views
0 votes
1 answer

How to get ethereum contract public variables?

You need to use call method, it ...READ MORE

answered Oct 17, 2018 in Blockchain by Christine
• 15,790 points
27 views
0 votes
1 answer

Ethereum nodejs: Contract address returning as undefined in console

As per the documentation, if you deploy the ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 67,480 points
201 views
0 votes
1 answer