0

I have a smart contract that basically store a document and I integrated it with a user interface that let the user go to the website and search for that document .. very simple idea.

I want to let the user use my website without having a wallet since no gas require (only read)

I tried creating an instance of the contract:

        var contract
        var addresse = //my contract addresse;
        var api = // api ;

        contract = new web3.eth.contract(api, addresse); 

but when a i tried to use the website it say:

(index):240 Uncaught TypeError: Cannot read property 'isEntity' of undefined

here my code:

var contract;

    var addresse = "0xDf6E7A02DC7457B7C1c534159305662B63794958";
    var api = [
{
    "constant": false,
    "inputs": [
        {
            "internalType": "string",
            "name": "stdID",
            "type": "string"
        }
    ],
    "name": "deleteCertificate",
    "outputs": [
        {
            "internalType": "bool",
            "name": "success",
            "type": "bool"
        }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
},
{
    "constant": false,
    "inputs": [
        {
            "internalType": "string",
            "name": "certID",
            "type": "string"
        },
        {
            "internalType": "string",
            "name": "stdID",
            "type": "string"
        },
        {
            "internalType": "string",
            "name": "stdName",
            "type": "string"
        },
        {
            "internalType": "uint256",
            "name": "nID",
            "type": "uint256"
        },
        {
            "internalType": "string",
            "name": "collage",
            "type": "string"
        },
        {
            "internalType": "string",
            "name": "stdGPA",
            "type": "string"
        },
        {
            "internalType": "string",
            "name": "gradYear",
            "type": "string"
        }
    ],
    "name": "newCertificate",
    "outputs": [
        {
            "internalType": "bool",
            "name": "success",
            "type": "bool"
        }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
},
{
    "inputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "constructor"
},
{
    "constant": true,
    "inputs": [
        {
            "internalType": "string",
            "name": "stdID",
            "type": "string"
        }
    ],
    "name": "getCartID",
    "outputs": [
        {
            "internalType": "string",
            "name": "",
            "type": "string"
        }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
},
{
    "constant": true,
    "inputs": [
        {
            "internalType": "string",
            "name": "certID",
            "type": "string"
        },
        {
            "internalType": "string",
            "name": "stdID",
            "type": "string"
        }
    ],
    "name": "getCertificateInfo",
    "outputs": [
        {
            "internalType": "string",
            "name": "",
            "type": "string"
        },
        {
            "internalType": "string",
            "name": "",
            "type": "string"
        },
        {
            "internalType": "uint256",
            "name": "",
            "type": "uint256"
        },
        {
            "internalType": "string",
            "name": "",
            "type": "string"
        },
        {
            "internalType": "string",
            "name": "",
            "type": "string"
        },
        {
            "internalType": "string",
            "name": "",
            "type": "string"
        }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
},
{
    "constant": true,
    "inputs": [
        {
            "internalType": "string",
            "name": "stdID",
            "type": "string"
        }
    ],
    "name": "isEntity",
    "outputs": [
        {
            "internalType": "bool",
            "name": "isIndeed",
            "type": "bool"
        }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}

]; contract = new web3.eth.contract(api, addresse);

    function getDou(){

        var certHolder = document.getElementById("certInput").value;
        var isExist = contract.methods.isEntity(certHolder).call().then(function (result) {
            if(result){
                contract.methods.getCartID(certHolder).call().then(function(result){   
                    document.getElementById("getDoc").innerHTML = result;
                });
            }else{
                alert("The document does not exist!!");
                location.reload();
            }
        }).catch(function (err) {
        console.log(err.message);
        });    
    }

note: the code work fine when I added this before the contract

web3 = new Web3(web3.currentProvider);

but i think this will not work for the users

  • You forgot to add the connection with your web3 provider. – Adam Boudjemaa Feb 26 at 0:54
  • if i added it how the user will use the website ... it will say to him:web3 is not defined – iias121 Feb 26 at 0:56
  • i upload it here with the connection with web3 provider and it's not working for the user only work for me. i3asim.000webhostapp.com – iias121 Feb 26 at 1:02
0

Ethers.js uses a different approach that I believe provides developers more flexibility.

Try with this code after npm i ethers and renaming your variable api-> abi

//Load ethers
const ethers = require('ethers');


async function isEntity(entity) {
// Connect to the network
const provider = ethers.getDefaultProvider("homestead");

// The address of the smart contract
const contractAddress = "0xDf6E7A02DC7457B7C1c534159305662B63794958";

// We connect to the Contract using a Provider, so we will only
// have read-only access to the Contract
const contract = new ethers.Contract(contractAddress, abi, provider);


return await contract.isEntity(entity);

}
| improve this answer | |
  • not working i got POST https://mainnet.infura.io/ 403 and etrh.js:1 Uncaught (in promise) Error: invalid response - 403 at XMLHttpRequest.o.onreadystatechange (etrh.js:1) – iias121 Feb 26 at 10:59
  • can you share full code? – Adam Boudjemaa Feb 26 at 11:06
  • I uploaded the original code and the code when I tried ether.js here: github.com/i3asim/search-doucment – iias121 Feb 26 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.