skip to Main Content

Basically, the weather API; Apixu changed everything to weatherstack recently, including their endpoints and I need help updating my twitter weather bot.

I did go through the documentation, changed to axios but I keep getting the “Cannot Read Property error”

My Old API Setup

   const Twit = require('twit');
   const config = require('./config');
   const rp = require('request-promise-native');

async function setup(location) {
    const options = {
    url: "http://api.apixu.com/v1/current.json",
    qs: { 
          key: API_KEY,
          q: location
        },
    json: true
};
    let result = await rp(options);
    let condition = result.current.condition.text;
    let tweetText = `The condition in ${location} is currently ${condition}!`;
    console.log("TWEETING : ", tweetText);
    sendTweet(tweetText)
}

According to their documentation, this is how it’s supposed to be but I keep getting undefined errors.

   const params = {
   access_key: 'YOUR_ACCESS_KEY',
   query: 'New York'
}

axios.get('https://api.weatherstack.com/current', {params})
   .then(response => {
   const apiResponse = response.data;
   console.log(`Current temperature in ${apiResponse.location.name} is ${apiResponse.current.temperature}℃`);
   }).catch(error => {
   console.log(error);
  });

The new Base URL: The new API requests start out with :

http://api.weatherstack.com/

documentation : https://weatherstack.com/quickstart

UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘c
ondition’ of undefined

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejection
id: 1)

2

Answers


  1. I would check the response.data.error object, if something goes wrong this will be populated. Funnily enough the http status code is still 200 for some error conditions.

    axios.get('https://api.weatherstack.com/current', {params})
        .then(response => {
            if (!response.data.error) {
                const apiResponse = response.data;
                console.log(`Current temperature in ${apiResponse.location.name} is ${apiResponse.current.temperature}℃`);
            } else {
                console.log(`Response error: code: ${response.data.error.code}, info: ${response.data.error.info}`)
            }
        }).catch(error => {
            console.error("An error occurred: ", error);
        }
    );
    

    Using the free tier, I’m getting the following error with this request:

    Response error: code: 105, info: Access Restricted - Your current Subscription Plan does not support HTTPS Encryption.
    

    This is easily worked around by changing to http only (This will be less secure!):

    axios.get('http://api.weatherstack.com/current', {params})
        .then(response => {
            if (!response.data.error) {
                const apiResponse = response.data;
                console.log(`Current temperature in ${apiResponse.location.name} is ${apiResponse.current.temperature}℃`);
            } else {
                console.log(`Response error: code: ${response.data.error.code}, info: ${response.data.error.info}`)
            }
        }).catch(error => {
            console.error("An error occurred: ", error);
        }
    );
    
    Login or Signup to reply.
  2. If you are using free version you need to use ‘http’ to work, i guess if you want to use ‘https’ it is premiun that you need to buy
    Here is the simple example that i have used
    http://api.weatherstack.com/current?access_key=0a82bdc4c6628b5f968dd500d30a8857&query=19.0760,-72.8777

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search