[Vuejs]-Nativescript playground vue.js show error message "TypeError: undefined is not an object (evaluating 'res.list.splice)"

0👍

Unless you changed the API key the response when requesting the forecast is:

{
  "cod": 401,
  "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."
}

Solution: You might have to enable your API key for each endpoint separately, because it does work for when requesting the current weather.

IMPORTANT: With the posted code block/linked code you exposed your API key. Make sure you reset/regenerate it and the current API key does no longer work.


The important thing to note is that fetch() within you Requestor does not check if the response is ok.

export function get(url) {
    return fetch(
        url
    ).then(function (response) {
        return response.json();
    }).then(function (json) {
        return json;
    });
}

The fetch() documentation says:

The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure or if anything prevented the request from completing.

This means like Requestor is currently written you should check the data.cod for a success response (200). Alternatively you could rewrite your Requestor to throw on a not-ok response.

// Return parsed JSON if response is ok. When the response is not ok
// reject with the response as error object.
export function get(url) {
  return fetch(url).then((response) => {
    if (!response.ok) throw response;
    return response.json();
  });
}

Leave a comment