2

I am trying to update and call data to remix using node js. I have the temperature captured in my js code and I am trying to call it using the updateWeather function.

Node.js code

let request = require('request');
let url = 'https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20%3D%202487889&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys'

request(url, function (err, response, body) {
  if(err){
    console.log('error:', error);
  } else {
    let weather = JSON.parse(body)
    let temperature = weather.query.results.channel.item.condition.temp.toString();
    let message = `It's ${temperature} in Hawaii!`;
    console.log(message);
  }
});

Web3 = require('web3')
if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
    } else {
        // set the provider you want from Web3.providers
        web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    }

    web3.eth.defaultAccount = web3.eth.accounts[0];

const contract = web3.eth.contract(ABI).at('ADDRESS');

const account = () => {
  return new Promise((resolve, reject) => {
    web3.eth.getAccounts((err, accounts) => {
      if (err === null) {
        resolve(accounts[0]);
      } else {
        reject(err);
      }
    });
  });
};

const updateWeather = ({temperature }) => {
  return new Promise((resolve, reject) => {
    account().then(account => {
      contract.updateWeather(temperature,{ from: account }, (err, res) => {
          if (err === null) {
            resolve(res);
          } else {
            reject(err);
          }
        }
      );
    }).catch(error => reject(error));
  });
};

const weatherUpdate = (callback) => {
  contract.WeatherUpdate((error, result) => callback(error, result));
};

Remix Code

pragma solidity 0.4.24;


contract WeatherOracle {

  event WeatherUpdate (
    string temperature
  );

  function updateWeather (string temperature) public {
      emit WeatherUpdate (
      temperature
      );
  }
}
  • Can you describe a problem, if you have any? – Xavier Leprêtre B9lab Jul 3 '18 at 8:53
  • The problem is that I cant send my temperature value to my remix contract – zak6 Jul 3 '18 at 21:59
  • Why do you have 2 methods to get accounts? web3.eth.defaultAccount = web3.eth.accounts[0]; and const account = ()? – Xavier Leprêtre B9lab Jul 5 '18 at 9:51
  • Have you checked that you got the right address here web3.eth.contract(ABI).at('ADDRESS');? Have you tried web3.eth.getCode('ADDRESS')? – Xavier Leprêtre B9lab Jul 5 '18 at 9:51
  • I don't know what is this syntax contract.WeatherUpdate((error, result) =>. – Xavier Leprêtre B9lab Jul 5 '18 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.