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,370 points
503 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
• 69,220 points

Related Questions In Blockchain

+1 vote
1 answer
0 votes
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,400 points
5,052 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
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
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
• 69,220 points
1,464 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
1,963 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
• 69,220 points
1,870 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP