Search code examples
javascriptjqueryvisual-studio-codesapui5sap-fiori

Cannot find name 'jQuery' error in controller.js


I am developing a UI5 app in VS Code. I added a new count function to the *.controller.js file, and in order to display the count from the server, I am using jQuery like in the following code:

jQuery.each(this._mFilters, function (sFilterKey, oFilter) {
  oModel.read("/portfolios/$count", {
    filters: oFilter,
    success: function (oData) {
      var sPath = "/" + sFilterKey;
      oViewModel.setProperty(sPath, oData);
    }
  });
});

Unfortunately, I get the following error: Screenshot of VSCode error

Does anyone know why was the error triggered and how it can be fixed?
Any help or suggestion is much appreciated.


Solution

  • I assume this._mFilters is an object. In that case, try with:

    Object.keys(this._mFilters).map(sFilterKey => {
      const oFilter = this._mFilters[sFilterKey];
      oModel.read("/portfolios/$count", {
        filters: [ oFilter ],
        success: function(sCount) {
          const sPath = `/${sFilterKey}`;
          oViewModel.setProperty(sPath, +sCount);
        },
      });
    });
    

    Also the parameter filters awaits an array instead of a single Filter instance.


    If jQuery is still preferred, include "sap/ui/thirdparty/jquery" (formerly "jquery.sap.global") to the dependency list of the controller.

    sap.ui.define([
      "sap/ui/core/mvc/Controller",
      // ...,
      "sap/ui/thirdparty/jquery",
    ], function(Controller,/*...,*/ jQuery) {
      // jQuery is here available
    });