javascriptsapui5ui5-toolingui5-webcomponents

Uncaught (in promise) SyntaxError: Unexpected token 'const' in UI5 (SAP)


Hi I wanted to make an API Call in Ui5 but everytime it tells me, that it doesnt know whats 'Const'.

Uncaught SyntaxError: Unexpected token 'const' The following error occurred while displaying routing target with name 'TargetResult': SyntaxError: Unexpected token 'const' -
Uncaught (in promise) SyntaxError: Unexpected token 'const'

my Ui5 call:

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

      return Controller.extend("TESTE.TESTE.controller.ResultDevice", {

            onInit: function() {
              var sUrl = "/api/tablets?limit=1000&offset=0";
              const url = "https://jsonplaceholder.typicode.com/users";

              fetch(url).then(res => res.json()).then(res => (
                  const dataModel = new JSONModel(); dataModel.setData({
                    items: res
                  }); this.getView().setModel(dataModel, "aribadevices")

                )
              },
            },

Solution

  • There are several issues with your curly braces and parenthesis. This code should work, doesn't have syntax errors, and is a little easier to read with each expression being on a new line.

    sap.ui.define([
        "sap/ui/core/mvc/Controller", "sap/ui/model/json/JSONModel"
      ], function(Controller, JSONModel) {
        "use strict";
    
        return Controller.extend("TESTE.TESTE.controller.ResultDevice", {
    
                onInit: function() {
                var sUrl = "/api/tablets?limit=1000&offset=0";
                const url = "https://jsonplaceholder.typicode.com/users";
    
                fetch(url).then(res => res.json()).then(res => {
                    const dataModel = new JSONModel(); 
                    dataModel.setData({
                        items: res
                    }); 
                    this.getView().setModel(dataModel, "aribadevices");
                })
            }
        })
    });