Cannot find name 'jQuery' error in controller.js

好久不见. 提交于 2021-01-28 18:36:51

问题


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:

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


回答1:


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
});


来源:https://stackoverflow.com/questions/63617557/cannot-find-name-jquery-error-in-controller-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!