Meteor Easysearch根据登录用户过滤用户数据

问题描述 投票:0回答:1

我正在尝试根据此处的文档[http://matteodem.github.io/meteor-easy-search/docs/recipes/][1]过滤传递给easysearch的集合的返回对象。这应该仅根据登录的userId过滤和返回记录。它返回错误ID的错误。

我不确切知道从哪里获取id,因为登录用户的userId与它正在搜索的内容不同。

这是easysearch设置

// Client and Server
SchoolStudentsIndex = new EasySearch.Index({

  engine: new EasySearch.MongoDB({
    sort: function () {
      return { createdAt: -1};
    },
    selector(searchDefinition, options, aggregation) {
      // retrieve the default selector
      var selector = this.defaultConfiguration().selector(searchObject, options, aggregation)

      // options.search.userId contains the userId of the logged in user
      selector.owner = options.search.userId

      return selector
    },
  }),
  collection: SchoolStudents,
  fields: ['firstname', 'lastname', 'username', 'middlename', 'studentclass', 'address', 'city', 'state', 'country', 'registra'],
  defaultSearchoptions: {
    limit: 8
  },
  permission: (options) => options.userId, // only allow searching when the user is logged in
});

错误返回

    I20170818-20:17:27.451(1)? Exception from sub schoolstudents/easySearch id RGBeuyLZybDvrwppj ReferenceError: searchObject is not defined
    I20170818-20:17:27.456(1)?     at MongoDBEngine.EasySearch.Index.engine.EasySearch.MongoDB.selector (lib/collections/student.js:50:59)
    I20170818-20:17:27.457(1)?     at MongoDBEngine.callConfigMethod (packages/easysearch:core/lib/core/engine.js:50:19)
    I20170818-20:17:27.458(1)?     at MongoDBEngine.getSearchCursor (packages/easysearch:core/lib/engines/mongo-db.js:83:27)
    I20170818-20:17:27.459(1)?     at MongoDBEngine.search (packages/easysearch:core/lib/core/reactive-engine.js:107:19)
    I20170818-20:17:27.460(1)?     at [object Object].<anonymous> (packages/easysearch:core/lib/core/search-collection.js:177:43)
    I20170818-20:17:27.461(1)?     at packages\matb33_collection-hooks.js:307:21
    I20170818-20:17:27.462(1)?     at [object Object]._.extend.withValue (packages\meteor.js:1122:17)
    I20170818-20:17:27.464(1)?     at [object Object]._handler (packages\matb33_collection-hooks.js:306:28)
    I20170818-20:17:27.468(1)?     at packages\check.js:130:16
    I20170818-20:17:27.471(1)?     at [object Object]._.extend.withValue (packages\meteor.js:1122:17)


  [1]: http://matteodem.github.io/meteor-easy-search/docs/recipes/

这是客户端设置

inputAttributes: function () {
    return {'class': 'easy-search-input', 'placeholder': 'Search Anything'};
  },

  players: function () {
    return SchoolStudents.find({'userId': Meteor.userId()}, {sort: {createdAt: -1} });
  }, 

  selectedName: function () {
    var students = SchoolStudentsIndex.config.mongoCollection.findOne({__originalId: Session.get('selectedStudents')});
    return students && students.firstname;
  },

  index: function () {
    return SchoolStudentsIndex;
  },

  resultCount: function () {
    return SchoolStudentsIndex.getComponentDict().get('count');
  }, 

  showMore: function () {
    return false;
  },
  renderTempl: function () {
    Template.renderTemplate
  }

});

Template.UserStudents.helpers({
  selected: function () {
    return Session.equals('selectedStudents', this.__originalId) ? 'selected' : '';
  }
});

这是模板设置

<template name="student">
    {{#if currentUser}}
  <div class="row uniform">
    <div class="12u$">
      <!-- search -->
  <div id="search-wrap">

     <!-- Easy Search -->
    <div class="row" style="margin-left: -50px; margin-top: -100px;">
    <div class="col-md-2">
      {{#unless uploading}}
        <input type="file" name="uploadCSV" class="special">
      {{else}}
        <p><i class="fa fa-spin fa-refresh"></i> Uploading files...</p>
      {{/unless}}
    </div>
      <div class="black searchbar col-md-8">{{> EasySearch.Input index=index attributes=inputAttributes }}</div>
      <button style="margin-right: 10px;" class="special" id="add-student-to-module" name="add-student-to-module">ADD TO MODULE</button>
    </div>
  <!-- search -->
    <!-- Easy Search -->
    <div><button name="invitestidents" class="button" id="invitestidents">INVITE STUDENTS</button></div>
    <div><input type="checkbox" checked data-toggle="toggle" value="Select All"></div>
    {{#EasySearch.IfInputEmpty index=index }}
      <div class="padded examples black">For example "Abraham Jide Chukwudi"</div>
    {{else}}
      {{#if resultCount}}
        <div class="padded count-results black">{{resultCount}} results found.</div>
      {{/if}}
    {{/EasySearch.IfInputEmpty}}

    {{#EasySearch.IfSearching index=index }}
      <div>Searching</div>
    {{/EasySearch.IfSearching}}

    <ol class="leaderboard">
      {{#EasySearch.Each index=index }}
        {{> UserStudents}}
      {{/EasySearch.Each}}  
    </ol>

    {{#EasySearch.IfNoResults index=index }}
      <div class="padded no-results black">No results found</div>
    {{else}}
    {{/EasySearch.IfNoResults}}

    {{> EasySearch.Pagination index=index maxPages=20 }}
    {{! > EasySearch.LoadMore index=index}}

    {{#if showMore}}
      {{> EasySearch.Input name="mini-index" index=index attributes=inputAttributes }}
      <ul>
        {{#EasySearch.Each name="mini-index" index=index}}
          <li>{{name}}</li>
        {{/EasySearch.Each}}
      </ul>

    {{/if}}
    <!-- Easy Search -->
  <!-- End search -->
  </div>
  </div>
  </div>
{{/if}}
</template>

<template name="UserStudents">

{{#if currentUser}}
  <div class="row" style="margin-top: -50px;">
    <div class="container-pad" id="property-listings">
            <div class="row">
                    <!-- Begin Listing: 609 W GRAVERS LN-->
                    <div class="brdr bgc-fff pad-10 box-shad btm-mrg-20 property-listing card-1">
                        <div class="media">
                        {{#if imagepath }}
                          <a class="pull-left" href="/student/{{slug}}" target="_parent">
                            <img alt="image" class="img-responsive" src="{{imagepath}}" style="width: 70px; height: 70px; object-fit: contain;"></a>
                        {{else}}
                            <a class="pull-left" href="/student/{{slug}}" target="_parent">
                            <img alt="image" class="img-responsive" src="/images/user_male_icon.png" style="width: 70px; height: 70px; object-fit: contain;"></a>
                        {{/if}}
                            <div class="clearfix visible-sm"></div>

                            <div class="media-body fnt-smaller">
                                <a href="#" target="_parent"></a>

                                <h4 class="media-heading">
                                  <a href="/student/{{slug}}" target="_parent">{{firstname}} {{lastname}} <small class="pull-right">{{ createAt}}</small></a></h4>


                                <ul class="list-inline mrg-0 btm-mrg-10 clr-535353">
                                    <li><strong> {{username}}</strong></li>

                                    <li style="list-style: none">|</li>

                                    <li><strong>{{email}}</strong> {{mobile}} </li>

                                    <li style="list-style: none"> | {{studentclass}}|</li>

                                    <li><strong>{{address}},  {{city}}, {{state}}, {{country}} </strong></li>
                                </ul>

                               <!-- <p class="hidden-xs">{{trimString addschoolmission 0 260}}</p><span class="fnt-smaller fnt-lighter fnt-arial">Courtesy of HS Fox & Roach-Chestnut Hill
                                Evergreen</span>-->
                            </div>
                        </div>
                    </div><!-- End Listing-->



                </div>


        </div><!-- End container -->

  </div>
{{/if}}
</template>

jSon数据库中的用户记录样本

{
    "_id" : "Ge5ApePFGw5g25mRm",
    "firstname" : "Kehinde",
    "lastname" : "Adeoya",
    "middlename" : "Adekusibe",
    "username" : "ken4ward",
    "password" : "CYfEJcFS",
    "useremail" : "[email protected]",
    "studentclass" : "ss2",
    "dateofbirth" : "3-Mar-00",
    "gender" : "m",
    "ethinicity" : "black",
    "mobile" : "8023472436",
    "address" : "1 Abrahamoivc",
    "city" : "bolson",
    "lg" : "loveland",
    "state" : "ekiti",
    "country" : "Ukraine",
    "registra" : "kadeoya",
    "userId" : "KgQZnwqqzWL6D9x2T",
    "createdAt" : ISODate("2017-08-18T19:17:39.593Z")
}
authentication meteor meteor-blaze userid meteor-easy-search
1个回答
1
投票

这看起来像一个错字,你有:

selector(searchDefinition, options, aggregation) {
  var selector = this.defaultConfiguration().selector(searchObject, options, aggregation)

我想你的意思是:

selector(searchDefinition, options, aggregation) {
  var selector = this.defaultConfiguration().selector(searchDefinition, options, aggregation)

无论是那个或你的意思是在某处定义searchObject但忘了。

© www.soinside.com 2019 - 2024. All rights reserved.