0

I'm learning the tutorial from https://github.com/LearnWeb3DAO/BasicFrontEndTutorial to create an Ethereum DApp with etherjs. This is my HTML file

<html>
<head>
 <link rel='shortcut icon' type='image/x-icon' href='/favicon.ico' />
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
      crossorigin="anonymous">

 <title>Ethersjs Demo</title>
 <style>
    body {
        background-color: black;
        text-align: center;
        color: white;
        font-family: Arial, Helvetica, sans-serif;
    }
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
        crossorigin="anonymous"></script>

 </head>

 <body>

<script charset="utf-8" src="https://cdn.ethers.io/scripts/ethers-v4.min.js" type="text/javascript">
</script>

<div class="container">
    <div class="form-group">
        <h1>This is my Ethersjs Demo!</h1>
        <p> Here we can set or get the mood: </p>
        <div class="row">
            <div class="col offset-md-4 col-md-4">
                <label for="mood">Input Mood:</label>
                <input type="text" class="form-control" id="mood">
            </div>
        </div>
        <div class="row mt-2">
            <div class="col offset-md-4 col-md-2">
                <button class="btn btn-primary btn-block" onclick="getMood()"> get Mood </button>
            </div>
            <div class="col col-md-2">
                <button class="btn btn-light btn-block" onclick="setMood()"> set Mood</button>
            </div>
        </div>
    </div>
</div>

<script>
    window.ethereum.enable()
    var provider = new ethers.providers.Web3Provider(web3.currentProvider, 'ropsten');

    var MoodContractAddress = "0x7D8CBd7De0b4651432b4367ce2B9E5201bB59c5b";
    let MoodContractABI = [
        {
            "constant": true,
            "inputs": [],
            "name": "getMood",
            "outputs": [
                {
                    "internalType": "string",
                    "name": "",
                    "type": "string"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [
                {
                    "internalType": "string",
                    "name": "_mood",
                    "type": "string"
                }
            ],
            "name": "setMood",
            "outputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        }
    ]

    provider.listAccounts().then(function (accounts) {
        signer = provider.getSigner(accounts[0]);
        MoodContract = new ethers.Contract(MoodContractAddress, MoodContractABI, signer);
    })

    async function getMood() {
        getMoodPromise = MoodContract.getMood();
        var Mood = await getMoodPromise;
        alert("The current mood is: " + Mood);
    }

    async function setMood() {
        mood = $("#mood").val()
        if (mood == "") {
            alert("Please input mood");
        } else {
            setMoodPromise = MoodContract.setMood(mood);
            await setMoodPromise;
        }
    }
</script>

And this is the window when I launched the html file enter image description here

When I tried to "Set Mood", the error

Uncaught (in promise) Error: contract not deployed (contractAddress="0x7D8CBd7De0b4651432b4367ce2B9E5201bB59c5b", operation="getDeployed", version=4.0.45)

How do I fix this problem? Thank you

1 Answer 1

0

Just as it says, "uncaught promise". Write a catch after your promise statements to receive the MetaMask error, in this case:

async function setMood() {
        mood = $("#mood").val()
        if (mood == "") {
            alert("Please input mood");
        } else {
            setMoodPromise = await MoodContract.setMood(mood)
                .catch((error) => { console.log(error); });
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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