If you getting errors bellow while trying to execute your truffle external script continue to read.
errors:
TypeError: Cannot read property 'apply' of undefined
exception at require.js:128:1
TypeError: fn is not a function
For some reason it was quite difficult to find solution how to run external script. After some time I finally have figure it out so I'm sharing it with world:
errors:
TypeError: Cannot read property 'apply' of undefined
exception at require.js:128:1
TypeError: fn is not a function
For some reason it was quite difficult to find solution how to run external script. After some time I finally have figure it out so I'm sharing it with world:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(callback) { | |
let Web3 = require('web3'); | |
const truffleContract = require('truffle-contract') | |
let contract = truffleContract(require('../build/contracts/MetaCoin.json')); | |
var provider = new Web3.providers.HttpProvider("http://localhost:8545"); | |
var web3 = new Web3(provider); | |
contract.setProvider(web3.currentProvider); | |
//workaround: https://github.com/trufflesuite/truffle-contract/issues/57 | |
if (typeof contract.currentProvider.sendAsync !== "function") { | |
contract.currentProvider.sendAsync = function() { | |
return contract.currentProvider.send.apply( | |
contract.currentProvider, arguments | |
); | |
}; | |
} | |
contract.deployed().then(function(instance){ | |
return instance.yourFunction(); | |
}).then(response => { | |
console.log(response); | |
}); | |
} |
Thank you. This helped me a lot. Looking forward to future posts!
ReplyDeleteThx MUCH!!!
ReplyDelete