首页 > 解决方案 > 尝试在区块链中部署 todolist,添加新任务时,我收到无效地址错误

问题描述

我正在使用 ETH 区块链技术制作一个简单的区块链。

我正在按照本教程制作一个简单的 todolist 。

我的 todolist 工作正常,我可以看到那里的任务,但是当我尝试添加新任务时,我收到以下错误:

Uncaught (in promise) Error: invalid address
    v http://localhost:3000/js/web3.min.js:2
    l http://localhost:3000/js/web3.min.js:2
    formatInput http://localhost:3000/js/web3.min.js:2
    formatInput http://localhost:3000/js/web3.min.js:2
    toPayload http://localhost:3000/js/web3.min.js:2
    e http://localhost:3000/js/web3.min.js:2
    sendTransaction http://localhost:3000/js/web3.min.js:2
    execute http://localhost:3000/js/web3.min.js:2
    synchronizeFunction http://localhost:3000/vendor/truffle-contract/dist/truffle-contract.js:206
    synchronizeFunction http://localhost:3000/vendor/truffle-contract/dist/truffle-contract.js:157
    promise callback*synchronizeFunction/< http://localhost:3000/vendor/truffle-contract/dist/truffle-contract.js:156
    createTask http://localhost:3000/js/app.js:124
    onsubmit http://localhost:3000/:1
web3.min.js:2:4288
    createTask http://localhost:3000/js/app.js:125
    AsyncFunctionThrow self-hosted:696
    (Async: async)
    onsubmit http://localhost:3000/:1

​

这是我的app.js

App = {
  loading: false,
  contracts: {},

  load: async () => {
    await App.loadWeb3();
    await App.loadAccount();
    await App.loadContract();
    await App.render();
  },

  // https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8
  loadWeb3: async () => {
    if (typeof web3 !== "undefined") {
      App.web3Provider = web3.currentProvider;
      web3 = new Web3(web3.currentProvider);
    } else {
      window.alert("Please connect to Metamask.");
    }
    // Modern dapp browsers...
    if (window.ethereum) {
      window.web3 = new Web3(ethereum);
      try {
        // Request account access if needed
        await ethereum.enable();
        // Acccounts now exposed
        web3.eth.sendTransaction({
          /* ... */
        });
      } catch (error) {
        // User denied account access...
      }
    }
    // Legacy dapp browsers...
    else if (window.web3) {
      App.web3Provider = web3.currentProvider;
      window.web3 = new Web3(web3.currentProvider);
      // Acccounts always exposed
      web3.eth.sendTransaction({
        /* ... */
      });
    }
    // Non-dapp browsers...
    else {
      console.log(
        "Non-Ethereum browser detected. You should consider trying MetaMask!"
      );
    }
  },

  loadAccount: async () => {
    // Set the current blockchain account
    App.account = web3.eth.accounts[0];
    console.log(App.account);
  },

  loadContract: async () => {
    // Create a JavaScript version of the smart contract
    const todoList = await $.getJSON("TodoList.json");
    App.contracts.TodoList = TruffleContract(todoList);
    App.contracts.TodoList.setProvider(App.web3Provider);

    // Hydrate the smart contract with values from the blockchain
    App.todoList = await App.contracts.TodoList.deployed();
    // console.log(todoList);
  },

  render: async () => {
    // Prevent double render
    if (App.loading) {
      return;
    }

    // Update app loading state
    App.setLoading(true);

    // Render Account
    $("#account").html(App.account);

    // Render Tasks
    await App.renderTasks();

    // Update loading state
    App.setLoading(false);
  },

  renderTasks: async () => {
    // Load the total task count from the blockchain
    const taskCount = await App.todoList.taskCount();
    const $taskTemplate = $(".taskTemplate");

    // Render out each task with a new task template
    for (var i = 1; i <= taskCount; i++) {
      // Fetch the task data from the blockchain
      const task = await App.todoList.tasks(i);
      const taskId = task[0].toNumber();
      const taskContent = task[1];
      const taskCompleted = task[2];

      // Create the html for the task
      const $newTaskTemplate = $taskTemplate.clone();
      $newTaskTemplate.find(".content").html(taskContent);
      $newTaskTemplate
        .find("input")
        .prop("name", taskId)
        .prop("checked", taskCompleted)
        .on("click", App.toggleCompleted);

      // Put the task in the correct list
      if (taskCompleted) {
        $("#completedTaskList").append($newTaskTemplate);
      } else {
        $("#taskList").append($newTaskTemplate);
      }

      // Show the task
      $newTaskTemplate.show();
    }
  },

  createTask: async () => {
    App.setLoading(true);
    const content = $("#newTask").val();
    await App.todoList.createTask(content);
    window.location.reload();
  },

  toggleCompleted: async (e) => {
    App.setLoading(true);
    const taskId = e.target.name;
    await App.todoList.toggleCompleted(taskId);
    window.location.reload();
  },

  setLoading: (boolean) => {
    App.loading = boolean;
    const loader = $("#loader");
    const content = $("#content");
    if (boolean) {
      loader.show();
      content.hide();
    } else {
      loader.hide();
      content.show();
    }
  },
};

$(() => {
  $(window).load(() => {
    App.load();
  });
});

在控制台中,它显示地址为:0xc5cfa0a0345f74e26cecfd8ec3a5cfa3843955ac

我正在使用 metamask 和 genache,我尝试将我的智能联系人连接到我的 memask 钱包,所以我知道我已连接,但我不确定为什么会收到此错误。

我应该删除我的元掩码然后再做一次吗?我试图在这里寻找这个解决方案,但这些解决方案大多是旧的,并没有使我需要做的事情。

帮助真的很感激。

标签: ethereumsoliditysmartcontractsmetamaskganache

解决方案


修复它,在 app.js 中你必须替换这一行:

App.account = web3.eth.accounts[0];

使用以下行:

web3.eth.defaultAccount=web3.eth.accounts[0]

推荐阅读