script.js:38未捕获的TypeError:无法读取未定义的属性“ slice”

时间:2019-05-21 13:01:35

标签: jquery-select2

在读取带有json数据的文件后,下拉列表中没有任何显示。当我在文本框中单击时,它会旋转并显示“正在搜索”。 我有将近22k的设备要显示。但是没有人出现。如果输入前几个字符,则会显示“找不到匹配项”。这使我相信我的JSON数据没有被填充。有人可以帮我将JSON文件加载到数据变量中吗?

here is the script.js:
(function() {
  // init select 2
  $('#test').select2({
  ajax: { 
    type: "GET",
    url: "data/gd2.json",
        dataType: 'json',
        data: function (params) {
        var query = {
          search: params.term,
            type: 'public',
    tags: "true",
            placeholder: 'search',
        allowClear: true,
            multiple: true
          }
     },  
     processResults: function(data, page) {
        return { results: JSON.parse(data) };
    }
  },
  // query with pagination
  query: function(q) {
     var pageSize,
       results,
       that = this;
       pageSize = 30; // or whatever pagesize
       results = [];
       if (q.term && q.term !== '') {
         results = _.filter(that.data, function(e) {
           return e.text.toUpperCase().indexOf(q.term.toUpperCase()) >= 0;
         });
       } else if (q.term === '') {
           results = that.data;
       }
       q.callback({
         results: results.slice((q.page - 1) * pageSize, q.page * pageSize),
         more: results.length >= q.page * pageSize,
      });
    },
  });
})();
Something wrong with my call and results does not return any data.  I think that is why the slice gives error.
Here is my HTML:
  <body>
  <div class="container">
    <div class="page-header">
      <h1>Select Devices for Maintenance</h1>
    </div>
    <input id="test" style="width:50%;" placeholder="Enter text and scroll for more results" />
  </div>
    <script type="text/javascript" src='js/jquery.min.js'></script>
    <script src="js/lodash.min.js"></script>
    <script src="js/select2.min.js"></script>
    <script src="js/script.js"></script>
  </body>


This is the error that I get from chrome:
Error:
script.js:38 Uncaught TypeError: Cannot read property 'slice' of undefined
    at Object.query (script.js:38)
    at d.updateResults (select2.min.js:22)
    at d.opening (select2.min.js:22)
    at d.open (select2.min.js:22)
    at d.<anonymous> (select2.min.js:22)
    at HTMLAnchorElement.<anonymous> (select2.min.js:21)
    at HTMLAnchorElement.dispatch (jquery.min.js:3)
    at HTMLAnchorElement.r.handle (jquery.min.js:3)
query @ script.js:38
updateResults @ select2.min.js:22
opening @ select2.min.js:22
open @ select2.min.js:22
(anonymous) @ select2.min.js:22
(anonymous) @ select2.min.js:21
dispatch @ jquery.min.js:3
r.handle @ jquery.min.js:3

我对编码还很陌生,因此,我们将不胜感激。

这是我的json数据文件的摘要:

{
  "results": [
    {
   "text": "APC-None",
   "children": [
     {
    "id": 472,
    "text": "device472"
     }
   ]
    }, {
   "text": "Aruba-WLC",
   "children": [
     {
    "id": 504,
    "text": "device504"
     }, {
    "id": 505,
    "text": "device505"
     }, {
    "id": 506,
    "text": "device506"
     }, {
    "id": 507,
    "text": "device507"
     }, {
    "id": 508,
    "text": "device508"
     }
   ]
],
"pagination": {
   "more": true
 }
}

0 个答案:

没有答案