无法从Meteor方法插入对象

时间:2016-01-28 14:17:29

标签: mongodb meteor meteor-collection2

早上好!试图做什么应该是Meteor中的简单插入遇到一些麻烦,但是传递object正在扼杀我。

我的网站上有一组正在运行的过滤器,但现在我想将这些过滤器的selector保存到一个集合中,以便我可以快速提取与多个过滤器匹配的文档。

问题似乎是传递包含已保存搜索的对象。该对象确实具有Collection2可能看作嵌套属性的内容(" addresses.state")但我已经得到了" blackbox:true"在架构上设置,所以我不知道它为什么会在那里看...

这是整个流程:

保存搜索名称的表单:

<template name="saveSearch">
  <div>
    <button class="btn btn-link" type="button" data-toggle="collapse" data-target="#saveSearchForm" aria-expanded="false" aria-controls="saveSearchForm">
      Advanced Search
    </button>
    <div class="collapse" id="saveSearchForm">
      <div class="well">
        // Display filters from the awesome meteor-tabular-filter package
        {{> expression_filter}}
        <form id="saveSearch" class="form-inline">
          <div class="form-group">
            <label for="searchName">Save Search</label>
            <input type="text" class="form-control" name="searchName" id="searchName" placeholder="Search Name">
          </div>
          <button type="submit" class="btn btn-primary">Save Search</button>
        </form>
      </div>
    </div>
  </div>
</template>

调用方法以插入搜索的模板事件:

Template.saveSearch.events({
  'submit #saveSearch' ( event, template ) {
    event.preventDefault();

    let queries = Session.get("filter_selector"),
        search = {
          name: String(template.find( "[name='searchName']" ).value),
          selector: queries
        };

    console.log('search: ' + JSON.stringify(search));

    Meteor.call( 'insertSearch', search, ( error, response ) => {
      if ( error ) {
        Bert.alert( error.reason, 'warning', 'growl-top-right' );
      } else {
        Bert.alert( 'Search saved', 'success', 'growl-top-right' );
      }
    });
  }
});

这里有几个不同的console.log();结果传递给方法:

1) {"name":"Alabama","selector":{"addresses.state":"AL"}}
2) {"name":"Alabama","selector":{"$and":[{"addresses.state":"AL"},{"lastName":"Knapp"}]}}

插入搜索的流星方法

// in both/methods/insert
Meteor.methods({
  insertSearch( doc ) {
    check( doc, Searches.simpleSchema() );

    var defaultProperties = {
      orgId    : Meteor.user().profile.currentOrg,
      createdBy: Meteor.userId(),
      createdAt: moment().toDate()
    };

    newDoc = _.extend(doc, defaultProperties);

    console.log('search to insert: ' + JSON.stringify(newDoc));

    try {
      var searchId = Searches.insert( newDoc, {bypassCollection2: true} );
      console.log('search insert successful');
      return searchId;
    } catch( exception ) {
      console.log('search insert failed');
      throw new Meteor.Error(500, 'Search failed to save.');
    }
  }
});

这里有几个不同的console.log();结果传递给插入:

1) {"name":"Alabama","selector":{"addresses.state":"AL"},"orgId":"testorg","createdBy":"DHo3vr3fiZbSYBJks","createdAt":"2016-01-28T13:38:27.499Z"}
2) {"name":"Alabama","selector":{"$and":[{"addresses.state":"AL"},{"lastName":"Knapp"}]},"orgId":"testorg","createdBy":"DHo3vr3fiZbSYBJks","createdAt":"2016-01-28T13:42:51.468Z"}

Searches集合的架构定义......

Searches = new Mongo.Collection( 'searches' );

Searches.allow({
  insert: () => false,
  update: () => false,
  remove: () => false
});

Searches.deny({
  insert: () => true,
  update: () => true,
  remove: () => true
});

let searchSchema = new SimpleSchema({
  name: {
    label: 'Search Name',
    type: String
  },
  selector: {
    label: 'Search Criteria',
    type: Object,
    blackbox: true,
    optional: true
  },
  orgId: {
    type: String,
    optional: true
  },
  createdBy: {
    type: String,
    label: "Created By",
    optional: true,
    autoValue: function() {
      return Meteor.userId()
    }
  },
  createdAt: {
    type: Date,
    label: "Created At",
    optional: true,
    autoValue: function () {
      if (this.isInsert) {
        return new Date;
      }
    },
    denyUpdate: true
  },
  updatedAt: {
    type: Date,
    label: "Updated At",
    optional: true,
    autoValue: function () {
      if (this.isInsert) {
        this.unset();
      } else {
        return new Date;
      }
    }
  }
});

Searches.attachSchema( searchSchema );

最后,这是来自Kadira的错误日志:

message: Search failed to save. [500] 
stack:
Error: Search failed to save. [500]
    at [object Object].Meteor.methods.insertSearch (both/methods/insert/searches.js:21:13)
    at [object Object].methodMap.(anonymous function) (packages/meteorhacks_kadira/lib/hijack/wrap_session.js:164:1)
    at maybeAuditArgumentChecks (livedata_server.js:1698:12)
    at livedata_server.js:708:19
    at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
    at livedata_server.js:706:40
    at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
    at livedata_server.js:704:46
    at tryCallTwo (/Users/allen/.meteor/packages/promise/.0.5.1.1u69k2a++os+web.browser+web.cordova/npm/node_modules/meteor-promise/node_modules/promise/lib/core.js:45:5)
    at doResolve (/Users/allen/.meteor/packages/promise/.0.5.1.1u69k2a++os+web.browser+web.cordova/npm/node_modules/meteor-promise/node_modules/promise/lib/core.js:171:13)

提前感谢您的帮助!

0 个答案:

没有答案