首页 文章

我的javascript代码无法连接到我的HTML代码(它只是没有出现在我的html网站上)

提问于
浏览
0

首先感谢您抽出宝贵时间阅读本文:

我一直在研究这个问题 . 我正在使用this教程视频在以太坊网络上创建自己的令牌/加密货币 . 你应该知道一些事情:

  • 我使用Ganache作为本地主机

  • 我正在使用Mac电脑

  • 我正在使用终端启动我的HTML网站,代码为 npm run dev

  • 我正在使用atom来编写我的所有代码

现在这里是我收到错误的javascript代码 . 特别是它说 App.contracts.CinoCoinSale.deployed().then(function(instance) 这个功能下的任何东西都不起作用 . 即使我做 console.log 它确实出现在我的控制台中 . 它真的很奇怪,我错了

App = {
  web3Provider: null,
  contracts: {},
  account: '0x0',
  loading: false,
  tokenPrice: 1000000000000000,
  tokensSold: 0,
  tokensAvailable: 750000,

  init: function() {
    console.log("App initialized...")
    return App.initWeb3();
  },

  initWeb3: function() {
    if (typeof web3 !== 'undefined') {
      // If a web3 instance is already provided by Meta Mask.
      App.web3Provider = web3.currentProvider;
      web3 = new Web3(web3.currentProvider);
    } else {
      // Specify default instance if no web3 instance provided
      App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
      web3 = new Web3(App.web3Provider);
    }
    return App.initContracts();
  },

  initContracts: function() {
    $.getJSON("CinoCoinSale.json", function(cinoCoinSale) {
      App.contracts.CinoCoinSale = TruffleContract(cinoCoinSale);
      App.contracts.CinoCoinSale.setProvider(App.web3Provider);
      App.contracts.CinoCoinSale.deployed().then(function(cinoCoinSale) {
        console.log("Cino Coin Sale Address:", cinoCoinSale.address);
      });
    }).done(function() {
      $.getJSON("CinoCoin.json", function(cinoCoin) {
        App.contracts.CinoCoin = TruffleContract(cinoCoin);
        App.contracts.CinoCoin.setProvider(App.web3Provider);
        App.contracts.CinoCoin.deployed().then(function(cinoCoin) {
          console.log("Cino Coin Address:", cinoCoin.address);
        });
        return App.render();
      });
    })
  },

  render: function() {
    if (App.loading) {
      return;
    }
    App.loading = true;

    var loader  = $('#loader');
    var content = $('#content');

    loader.show();
    content.hide();

    // Load account data
    web3.eth.getCoinbase(function(err, account) {
      if(err === null) {
        App.account = account;
        $('#accountAddress').html("Your Account: " + account);
      }
    })

    // Load token sale contract
    App.contracts.CinoCoinSale.deployed().then(function(instance) {
      cinoCoinSaleInstance = instance;
      return cinoCoinSaleInstance.tokenPrice();
    }).then(function(tokenPrice) {
      App.tokenPrice = tokenPrice;
      $('.token-price').html(web3.fromWei(App.tokenPrice, "ether").toNumber());
      return cinoCoinSaleInstance.tokensSold();
    }).then(function(tokensSold) {
      App.tokensSold = tokensSold.toNumber();
      $('.tokens-sold').html(App.tokensSold);
      $('.tokens-available').html(App.tokensAvailable);

      var progressPercent = (Math.ceil(App.tokensSold) / App.tokensAvailable) * 100;
     $('#progress').css('width', progressPercent + '%');

     // Load token contract
     App.contracts.CinoCoin.deployed().then(function(instance) {
       cinoCoinInstance = instance;
       return cinoCoinInstance.balanceOf(App.account);
     }).then(function(balance) {
       $('.cino-balance').html(balance.toNumber());
     })
   });

   App.loading = false;
   loader.hide();
   content.show();
  }
 },

 $(function() {
   $(window).load(function() {
     App.init();
   })
 });

然后这是我用来在localhost服务器上运行我的网站的HTML代码 . 该网站看起来非常好,它的工作除了一件事:没有出现的部分,我遇到的问题是从以前的代码插入值 . 这可能会让人感到困惑,所以这里有一个例子:对于 class="tokens-sold" 部分代码 <p><span class="tokens-sold"></span> / <span class="tokens-available"></span> tokens sold</p> 应该为 class="tokens-sold" 部分插入值, class="tokens-available" 这些值应该从我认为的Javascript文件中插入但是它不起作用因此不显示网站 .

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Cino Coin ICO Sale</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
    <div class="container" style="width: 650px;">
      <div class="row">
        <div class="col-lg-12">
            <h1 class="text-center">CINO COIN ICO SALE</h1>
            <hr/>
            
</div> <div id="loader"> <p class="text-center">Loading...</p> </div> <div id="content" class="text-center" style="display: none;"> <p> Introducing "Cino Coin" (CC)! Token price is <span class="token-price"></span> Ether. You currently have <span class="dapp-balance"></span>&nbsp;CC's. </p>
<form onSubmit="App.buyTokens(); return false;" role="form"> <div class="form-group"> <div class="input-group"> <input id="numberOfTokens" class="form-control input-lg" type="number" name="number" value="1" min="1" pattern="[0-9]"> </input> <span class="input-group-btn"> <button type="submit" class="btn btn-primary btn-lg">Buy Tokens</button> </span> </div> </div> </form> <br> <div class="progress"> <div id="progress" class="progress-bar progress-bar-striped active" aria-valuemin="0" aria-valuemax="100"> </div> </div> <p><span class="tokens-sold"></span> / <span class="tokens-available"></span> tokens sold</p> <hr> <p id="accountAddress"></p> </div> </div> </div> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> .</script> <script src="js/bootstrap.min.js"></script> <script src="js/web3.min.js"></script> <script src="js/truffle-contract.min.js"></script> <script src="js/app.js"></script> </body> </html>

如果找到解决方案,我会非常感激 . 如果您需要更详细的问题是什么,或者您不理解问题,那么只需说,我将尝试以不同的方式解释它 .

此外,here是我正在使用的视频 . 我在时间11:48被卡住,其中插入了代币价格 . 我已经完成了剩下的视频系列,到目前为止我的代码已经完成了 .

再次非常感谢你,我真的很感激:)

Here is the project folder

This is a snapshot of the folder

Edit

我做了一些研究,发现 App.contracts.CinoCoinSale.deployed().then(function(instance) { 下的任何东西都不起作用 . 即使我把 console.log("hi"); 它也不会打印到控制台 . 我仍然不知道如何解决这个问题但也许有帮助 . 它就像出于某种原因忽略了这个功能

1 回答

  • 0

    检查 App.contracts 下的内容可能没有正确加载 . 有's no way that it doesn'吨可以使用 Contract 对象 . 最坏的情况应该打印出你正在尝试的简单控制台 . 试试 console.log(App.contracts);

相关问题