Truffle Introduction

@CryptoBuddha
No worries…
Take a deep breathe, this patience will bear amazing fruits

go to truffle.js

add this in networks, make sure the port ganache is running is 7545 or change it whatever it is

module.exports = {
 
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*"
    }
}
1 Like

Hi - I have spent a good few hours trying to get truffle to run on my mac. I saw a few similar problems in the earlier comments, but those solutions did not seem to help me. Any advice appreciated.

Now I get the following:

2_Helloworld_deploy.js
======================

   Replacing 'Helloworld'
   ----------------------
   > transaction hash:    0x78b1c9e0c15dab5e78bf1cef4acd8205085893e09a3b45cc9b818de2ccd1098a
   > Blocks: 0            Seconds: 0
   > contract address:    0xd138E1aF1Cf02BA6f47A18dCAbA5ee10F1c2a4f4
   > block number:        13
   > block timestamp:     1595190068
   > account:             0x8a09239A9eb723F114Ac8e94580C7BDD36131493
   > balance:             99.95402234
   > gas used:            297116
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00594232 ETH


Error: Error: ReferenceError: accounts is not defined
    at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:96:1)
    at process._tickCallback (internal/process/next_tick.js:68:7)
Truffle v5.0.42 (core: 5.0.42)
Node v10.15.2

@Lamb
try

  1. run npm init and make a new npm project
  2. run npm i truffle
  3. run ./node_modules/.bin/truffle init and it should work!

Let me know if it works :slight_smile:
There are various work around to resolve this error and also difficult to create it, so All the best

@CryptoBuddha
In your index.js line number 96, the accounts variable is NULL or not created only.

I don’t understand what you are saying. I haven’t touched index.js. Please advise.

@CryptoBuddha
So wherever your accounts variable is, it is not defined in your code.
If you can give me the Github Link I could directly point out :slight_smile:

I am following the code used by Filip in the videos.

Here is my 2_Helloworld_deploy.js file where the “accounts” is used.

const Helloworld = artifacts.require("Helloworld");

module.exports = function(deployer) {
  deployer.deploy(Helloworld).then(function(instance){
  	instance.setMessage("Hello Again!", {value: 1000000, from: accounts[0]}).then(function(){
		instance.getMessage().then(function(message){
			console.log("Current message: " + message);
		});
	});
  });
};

here is the accounts[0]
It is an array, of your Ganache account which is undefined :slight_smile:
Meaning your accounts are not connected to your dApp

How do I connect my accounts to my dApp?

Try this

const Helloworld = artifacts.require("Helloworld");

module.exports = function(deployer) {
  deployer.deploy(Helloworld).then(function(instance, network, accounts){
  	instance.setMessage("Hello Again!", {value: 1000000, from: accounts[0]}).then(function(){
		instance.getMessage().then(function(message){
			console.log("Current message: " + message);
		});
	});
  });
};

I tried it and now get the following error:

Error: Error: TypeError: Cannot read property '0' of undefined
    at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:96:1)
    at process._tickCallback (internal/process/next_tick.js:68:7)
Truffle v5.0.42 (core: 5.0.42)
Node v10.15.2

@CryptoBuddha
You are very close, don’t worry try to debug the error, there is for sure something very small missing.
make sure you are connected to Ganache, check truffle-config.js
Best thing will if you can upload your code on GitHub and give me the link.

OK, I created a repository on GitHub. Here is the link: https://github.com/CryptoBuddha/HelloWorld

Hi Folks, Hi Bros, thanks for that great course and the community.
I got some issues and stuck a little bit with console.log. Can somebody give me a hint?
I dont get the console.log output using this:

const Helloworld = artifacts.require("Helloworld");
module.exports = function(deployer) {
    deployer.deploy(Helloworld).then(function(){
        console.log("contract deployed");
    });
};

using: Windows 10 64bit (all program versions according video tutorial)

OK. Issue solved using another truffle version like proposed in the forum
npm un -g truffle
npm i -g truffle@nodeLTS

3 Likes

Hello @CryptoBuddha

The “accounts” variable should be called in the main function, truffle is injecting few parameters during the migration as you can see here

For clarity i ll also recommend you to call the setMessage first then to call the getMessage.

const Helloworld = artifacts.require("Helloworld");

module.exports = function(deployer, accounts) {
  deployer.deploy(Helloworld).then(function(instance){
    instance.setMessage("Hello Again!", {value: 1000000, from: accounts[0]}).then(function(){
      console.log("Set message");
    });
    
    instance.getMessage().then(function(message){
      console.log("Current message: " + message);
    });
  });
};
3 Likes

@filip, Hi Filip, I’m really stuck here. I follow your instructions step by step for deploying contract in truffle but I get error message when I try to call the “getMessage” function.


This is the error message. I’ve tried the other suggestion of running truffle migrate --reset but it didn’t work.

Here’s the solidity code:

pragma solidity 0.5.12;

contract helloWorld {

  string public message = "Hello World";

  function getMessage() public view returns (string memory){
    return message;
  }

}

and here’s the migration code:

const helloWorld = artifacts.require("helloWorld");

module.exports = function (deployer) {
  deployer.deploy(helloWorld);
};

@Rafael21
Thanks for reaching out!
try this:
helloWorld.deployed().then(function(instance){helloworld=instance})
then
helloworld.getMessage()

If you are running the latest version of Truffle (v.3.x), the way to call a function or variable in your contract has evolved a little bit according to the Upgrading documentation

1 Like

@filip, Hey Filip! I am having some trouble with Node.js and creating the file and directory using Windows. Do you have any suggestions on a walk through on Node.js for Windows that could help with this? Thanks :smiley: