0

I am trying to develop a weather app which consumes 3rd party services (API link here ) for which the code is given in this link. When I run the app, it shows ther table with no data. It also shows as error in my View1.Controller file which I have provided below.

View1.controller.js

        sap.ui.define([
        "sap/ui/core/mvc/Controller",
        "sap/ui/model/json/JSONModel"
    ], function(Controller) {
        "use strict";

        return Controller.extend("WeatherAPI.controller.View1", {


    onInit: function() {
                this._loadForecast();
            },

            _formatDate: function(date) {
                var d = new Date(date),
                    month = '' + (d.getMonth() + 1),
                    day = '' + d.getDate(),
                    year = d.getFullYear();

                if (month.length < 2) {
                    month = '0' + month;
                }
                if (day.length < 2){
                    day = '0' + day;    
                } 
                return [year, month, day].join('-');
            },

            _mapResults: function(results) {
                var oModel = this.getView().getModel();
                oModel.setProperty("/city", results.city.name);
                oModel.setProperty("/country", results.city.country);

                var aForecastResults = [];
                for (var i = 0; i < results.list.length; i++) {
                    var oTemp = results.list[i].temp;
                    var date = this._formatDate(results.list[i].dt * 1000);
                    aForecastResults.push({
                        date: date,
                        temp: oTemp.day,
                        units: "Celsius",
                        humidity: results.list[i].humidity
                    });
                }

                oModel.setProperty("/items", aForecastResults);
            },

            _loadForecast: function() {
                var oView = this.getView();
                var oParams = {
                    q: "London",  // Get the weather in london
                    units: "metric", 
                    appid: "fa2a1fb5db6debe044492728ca66a8f8",  // replace with your API key
                    cnt: 16,  // get weather for the next 16 days
                    mode: "json"  // get it in JSON format 
                };
                var sUrl = "/OpenWeather/data/2.5/forecast/daily";
                oView.setBusy(true);

                var self = this;

                $.get(sUrl, oParams)
                    .done(function(results) {
                        oView.setBusy(false);
                        self._mapResults(results);
                    })
                    .fail(function(err) {
                        oView.setBusy(false);
                        if (err !== undefined) {
                            var oErrorResponse = $.parseJSON(err.responseText); /*error : Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>) */
                            sap.m.MessageToast.show(oErrorResponse.message, {
                                duration: 6000
                            });
                        } else {
                            sap.m.MessageToast.show("Unknown error!");
                        }
                    });
            }
        });
    });

model.js

        sap.ui.define([
        "sap/ui/model/json/JSONModel",
        "sap/ui/Device"
    ], function(JSONModel, Device) {
        "use strict";

        return {

            createDeviceModel: function() {
                var oModel = new JSONModel(Device);
                oModel.setDefaultBindingMode("OneWay");
                return oModel;
            },

            createAPIModel: function() {
                var oModel = new JSONModel();
                oModel.setData({
                    city: "",
                    country: "",
                    cols: [{
                        name: "Date"
                    }, {
                        name: "Day Temperature"
                    }, {
                        name: "Units"
                    },{
                        name: "humidity"
                    }],
                    items: []
                });
                return oModel;          
            }


        };
    });

neo-app.json

 {
  "path": "/OpenWeather",
  "target": {
    "type": "destination",
    "name": "OpenWeather",
    "entryPath": "/"
  },
  "description": "Open weather API"}

Please help. Thanks.

3
  • What error is shown ? Please post the exact error message you retrieve also with the line number, where the error happens. Jan 23, 2018 at 7:52
  • @FrancescoIannazzo I have mentioned the error in the code of View1.controller.js in a comment.
    – Swappy
    Jan 23, 2018 at 8:27
  • try to inspect the response, it seems to be html not json Chrome > Developer Toolbar > Network and search for the get call and try to open it in a new browser tab Jan 23, 2018 at 8:52

1 Answer 1

0

If you app is calling an API outside your domain, you will run into CORS issues. Maybe this is the case. Your network developer window will help you to figure this out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.