I have a contract :
pragma solidity ^0.4.2;
import "Owned.sol";
contract SGBFactory is Owned {
event SGBUpdate(uint sbgID, bool isCreated, string message); // isCreated = true means created, else means deleted
string public ownerType;
struct SGB {
uint id;
string latitude;
string longitude;
uint minBal; // should be in Wei since Solidity doesn't support floating points as of now
string ownerType;
uint capacity; //should also be in grams due to the same reasons as above
address owner;
uint fixedUnitCostPerTransaction;
}
SGB[] public sgbCollection;
function SGBCreator() {
ownerType = "COMMUNITY";
}
function addSGB(
string lat,
string lon,
uint minBalReq,
uint maxCapacity,
address ownerAccount,
uint fixedCost
) onlyOwner {
uint sgbID = sgbCollection.length++; //note the postfix operator here
sgbCollection[sgbID] = SGB(sgbID, lat, lon, minBalReq, ownerType, maxCapacity, ownerAccount, fixedCost);
// fire up an event here
SGBUpdate(sgbID,true, "A new SGB is created!!");
}
function getSGB(uint sgbid) returns (uint sbgID, string lat, string lon, uint minBal,string ownerType) {
return(
sgbCollection[sgbid].id,
sgbCollection[sgbid].latitude,
sgbCollection[sgbid].longitude,
sgbCollection[sgbid].minBal,
sgbCollection[sgbid].ownerType
);
}
// uint maxCapacity, address owner, uint fixedCost
// sgbCollection[sgbid].capacity,sgbCollection[sgbid].owner,sgbCollection[sgbid].fixedUnitCostPerTransaction
}
It is deployed in my private Ethereum chain. I can call getSGB() method from Truffle console and it returns value successfully.
I used the following mechanism in Truffle console:
var contract = SGBFactory.deployed();
contract.getSGB.call(1, {from: "0x85ded4eae096d15de9bee720a6e91eb28603a7e6"}, function(err, res) {
console.log(res);
});
But when I call the same method from JS file, the method returns nothing, nor there is an error message.
SGBWatcher.js
var accounts;
var coinbase_account;
function getSGBFromFactory() {
var sgbAdderContract = SGBFactory.deployed();
console.log(coinbase_account);
sgbAdderContract.getSGB.call(0, {from: coinbase_account }, function(err, res) {
if (err != null)
console.log(err);
else
console.log(res);
});
}
window.onload = function() {
web3.eth.getAccounts(function(err, accs) {
if (err != null) {
alert("There was an error fetching your accounts.");
console.log(err);
return;
}
if (accs.length == 0) {
alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
return;
}
accounts = accs;
coinbase_account = accounts[0];
});
}
What could be the issue here? (I already have two SGBs added in the contract, so the indexes 0 and 1 as parameter should be fine.)