I am developing a dapp using 'testrpc' and 'truffle'.I have also used ethereum-bridge for Oraclize.I have written a smart contract function which calls a url using 'OraclizeQuery'.I have added the code in app.js file, but the query won't get executed.I tried to execute the functions using truffle console and it worked well.
The following is the contract code :-
pragma solidity ^0.4.0;
import "./usingOraclize.sol";
contract WeatherApiCall is usingOraclize {
string public weathercondition;
function WeatherApiCall() {
OAR = OraclizeAddrResolverI(0x3df0db5bda9d685e41cb4a8834c44f8028957417);
}
function __callback(bytes32 myid, string result) {
if (msg.sender != oraclize_cbAddress()) throw;
weathercondition=result;
}
function update(string to,string datetime) payable returns(bool sufficient) {
oraclize_query("URL", strConcat("json(http://api.openweathermap.org/data/2.5/forecast?q='", to ,"'&mode=json&APPID=d2e8279188c8649c17540f798c9cc972).list[?(@.dt_txt='", datetime, "')].weather[0].main"));
return true;
}
}
The following is the index.html file:-
<!DOCTYPE html>
<html>
<head>
<title>Sample Truffle App</title>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link href="./app.css" rel='stylesheet' type='text/css'>
<script src="./app.js"></script>
</head>
<body>
<h1>Airline</h1>
<h2>Example Truffle Dapp</h2>
<br>
<h1>FORM</h1>
<br><label for="from">From:</label><input type="text" id="from" placeholder="--Residing Place--"></input>
<br><label for="to">To:</label><input type="text" id="to" placeholder="--Travelling Place--"></input>
<br><label for="persons">No. of Persons:</label><input type="text" id="persons" placeholder="--No. of Persons--"></input>
<br><label for="date">Travel Date:</label><input type="text" id="date" placeholder="--Date of travel yyyy-MM-dd--"></input>
<br><label for="time">Destination Time:</label><input type="text" id="time" placeholder="--HH:mm:ss--"></input>
<br><br><button id="send" onclick="sendRequest()">Generate Quote</button>
<br><br>
</body>
</html>
The following is the app.js file for the corresponding smartcontract code:-
var accounts;
var account;
function sendRequest() {
var weatherapicall = WeatherApiCall.deployed();
var fromdata = document.getElementById("from").value;
var to = document.getElementById("to").value;
var persons = parseInt(document.getElementById("persons").value);
var date = document.getElementById("date").value;
var time = document.getElementById("time").value;
//verifying at javascript console
console.log(fromdata);
console.log(to);
console.log(persons);
console.log(date);
console.log(time);
var timeperiod;
if ((time >= '00:00:00') && (time < '03:00:00')) {
timeperiod = '00:00:00';
} else if ((time >= '03:00:00') && (time < '06:00:00')) {
timeperiod = '03:00:00';
} else if ((time >= '06:00:00') && (time < '09:00:00')) {
timeperiod = '06:00:00';[![enter image description here][1]][1]
} else if ((time >= '09:00:00') && (time < '12:00:00')) {
timeperiod = '09:00:00';
} else if ((time >= '12:00:00') && (time < '15:00:00')) {
timeperiod = '12:00:00';
} else if ((time >= '15:00:00') && (time < '18:00:00')) {
timeperiod = '15:00:00';
} else if ((time >= '18:00:00') && (time < '21:00:00')) {
timeperiod = '18:00:00';
} else if ((time >= '21:00:00') && (time < '24:00:00')) {
timeperiod = '21:00:00';
}
datetime = date + ' ' + timeperiod;
console.log(datetime);
weatherapicall.update(to ,datetime, {from: account}).then(function(value) {
console.log("Testing");
console.log(value);
console.log(value.valueOf());
}).catch(function(e) {
console.log(e);
});
};
window.onload = function() {
web3.eth.getAccounts(function(err, accs) {
if (err != null) {
alert("There was an error fetching your accounts.");
return;
}
if (accs.length == 0) {
alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
return;
}
accounts = accs;
account = accounts[0];
});
}
I have loaded the dapp and provided the inputs and printed the values correctly at javascript console.But the call to update function won't get executed.