0

I have a function in my smart contract where I am trying to return some data that has been stored in the blockchain,

    function showOrg(address org) external view returns(uint international_securities_identifier, string memory organization_name, bool hasActivityStatus, string memory businessClassifier, string memory RegisteredAddress, string memory isDomiciledIn, bool sanStatus){
    require(orgs[org].admins[msg.sender] == true, "You are not authorized to view this data!");
    return(orgs[org].international_securities_identifier, orgs[org].organization_name, orgs[org].hasActivityStatus, orgs[org].businessClassifier, orgs[org].RegisteredAddress, orgs[org].isDomiciledIn, orgs[org].sanStatus);
}

I am using the following to call this function,

var result = AMLContract.methods.showOrg('0xCe37A39e3EaB674572EDd4b37f33841774750b2F').send({from: contractAddress})
console.log(result);

However, what I keep getting is a Promise object, is there any way that I can view the actual data? Or is my approach wrong?

Minura Punchihewa
  • 1,498
  • 1
  • 12
  • 35
  • 2
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – evolutionxbox Aug 04 '21 at 15:09
  • Do you think you could explain how I can use this in the code I have given above? I am honestly not very fluent in my JavaScript. – Minura Punchihewa Aug 04 '21 at 15:18
  • Consider reading through the answers? Some of them are very thorough and give a guide. – evolutionxbox Aug 04 '21 at 15:19
  • Honestly, it does not help much.. JavaScript is very new to me. I just know my Solidity. – Minura Punchihewa Aug 04 '21 at 15:38
  • You can only access the value of a resolved promise within a `.then`, or by using `await` inside an async function. If this makes no sense to you then read https://stackoverflow.com/questions/29516390/how-to-access-the-value-of-a-promise – evolutionxbox Aug 04 '21 at 15:40
  • @MinuraPunchihewa javaScript and Solidity are pretty similar. If you know solidity you could probably beef up your JS to a formidable level in 1 to 2 days. Checkout https://www.w3schools.com/. – pgSystemTester Aug 04 '21 at 15:47
  • @evolutionxbox one of the solutions given there helped me convert the Promise to an Object, but it still does not have the data that I am looking for? As given in the Solidity function that I have written, I am expecting the 'organization_name' and other fields. If I am not getting that, does that mean that something is wrong with my code? – Minura Punchihewa Aug 04 '21 at 15:52
  • I don't know. We cannot debug what we cannot see. – evolutionxbox Aug 04 '21 at 15:53

2 Answers2

1

You need a callback function to resolve a promise. Use your function like this

AMLContract.methods.showOrg('0xCe37A39e3EaB674572EDd4b37f33841774750b2F')
  .send({from: contractAddress}).then(function(receipt){
      console.log(receipt)
  });

receipt will hold the information about the transaction. And you can use events in your smart contract to emit the desired information from any function and then catch them with Web3.

Read more about Web3.js functionality here

Taimoor
  • 423
  • 3
  • 9
  • With your method, I get something like this, {transactionHash: "0x8b8bd00906b9cf2d5033a505a510f8d1777e60611a2d27e5a95f38612af0bf0c", transactionIndex: 0, blockHash: "0x733c8d0d5014204fbaf7c8a97a01506a7fa582542c9174179d5dc1451789c2d2", blockNumber: 42, from: "0xf7642c725fa20c2ff18befbf7351b5fb94077ef9", …} blockHash: "0x733c8d0d5014204fbaf7c8a97a01506a7fa582542c9174179d5dc1451789c2d2" blockNumber: 42 contractAddress: null cumulativeGasUsed: 21432 events: [[Prototype]]: Object constructor: ƒ Object() This is only part of the object. I just cannot see where the data I am expecting is.. – Minura Punchihewa Aug 04 '21 at 21:48
  • What you are asking now is a different issue. The receipt object is working fine as it is returning you the information of the transaction. Also looking at your function it is a view function and not a payable one. Since it is not changing the state of the contract, you can use call instead of send. And by using call, you will be able to see those returned values – Taimoor Aug 05 '21 at 11:48
1

I think this you should use await before calling the view function I solved my problem with this thing

const demo=await democontract.fn(params...)
   cosole.log(demo)

because if you call contracts function without its is going to return a pending promise :)

baibars313
  • 17
  • 3