【问题标题】:Meteor Method Insert into collection when collection.find().count() === 0当 collection.find().count() === 0 时 Meteor 方法插入到集合中
【发布时间】:2015-07-26 12:26:11
【问题描述】:

我有一个集合,我只想在 Collection.find().count() === 0 时插入数据。我正在使用流星方法进行插入,这样我也可以插入 Meteor 用户 ID,我的最终目标是在该特定集合上运行更新函数,我开始工作了,但是执行初始插入的方法继续运行即使它是 if 语句中的包装器,仅在集合为空时才这样做。

所有内容都是发布和订阅的,还有 Financials.allow 和 deny 代码,以及带有 ownsDocument 的权限文件。如果需要,我会发布所有代码。

同样在控制台中我收到此错误,尽管该方法正在插入数据,即使集合有一个文档,我只希望它在它为空时插入,因此几乎可以进行全新安装.

提前感谢您的帮助。

Exception while simulating the effect of invoking 'financialUserId' TypeError: Cannot read property '_id' of undefined {stack: (...), message: "Cannot read property '_id' of undefined"} TypeError: Cannot read property '_id' of undefined
    at Meteor.methods.financialUserId (http://localhost:3000/lib/collections/financials.js?0fde44b180e856bc334a164ad9859e394fd9578d:22:19)
    at http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:4269:25
    at _.extend.withValue (http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:955:17)
    at _.extend.apply (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:4260:54)
    at _.extend.call (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:4138:17)
    at http://localhost:3000/client/templates/editable/client_edit.js?578e3f500b0c73d3d22e659540d653cb6309cd52:9:8
    at http://localhost:3000/client/templates/editable/client_edit.js?578e3f500b0c73d3d22e659540d653cb6309cd52:15:3

客户端文件夹 /client/editable/client_edit.js

if (Meteor.isClient) {

  if (Financials.find().count() === 0) {

    var financial =   {issuedOutstanding: 666}  ;

    Meteor.call('financialUserId', financial, function(error, result)  {});

  }

}

/client/editable/edit_financials.js

Template.financialEdit.events({
  'submit form': function(e) {
    e.preventDefault();

    var currentFinanceId = this._id;


    var financialsProperties = {
      issuedOutstanding: $('#issuedOutstanding').val()



    };

    Financials.update(currentFinanceId, {$set: financialsProperties}, function(error) {
      if (error) {
        console.log(currentFinanceId);
        console.log(error);
        alert(error.reason);
      } else {
        console.log(financialsProperties);
        // Router.go('financials');
        //Router.go('financials');
        Router.go('financials', {_id: currentFinanceId});

      }
    });

  }

});

Lib 文件夹 /lib/collections/financials.js

Meteor.methods({
  financialUserId: function(eventAttributes) {



    var user = Meteor.user();
    var financial = _.extend(eventAttributes, {
      userId: user._id
    });
    var financialId = Financials.insert(financial);
    return {
      _id: financialId
    };
  }
});

【问题讨论】:

    标签: javascript mongodb meteor meteor-collections


    【解决方案1】:

    尝试将 Financials.js 移动到您的 /server 目录或将 Meteor.methods 代码包装在“if (Meteor.isServer)”条件中,以确保它在服务器上运行:

    if (Meteor.isServer){
     Meteor.methods({
      financialUserId: function(eventAttributes) {
    
    
    
        var user = Meteor.user();
        var financial = _.extend(eventAttributes, {
          userId: user._id
        });
        var financialId = Financials.insert(financial);
        return {
          _id: financialId
        };
      }
     });
    }
    

    如果要在客户端运行该方法,请使用 Meteor.userId() 作为 userId 的值。

    【讨论】:

    • 如果您阅读了我的上一条评论,我删除了我知道不再收到错误的信息,但是每次刷新页面时都会在集合中插入一个新字段,只有在集合时才应该这样做.find 等于一。
    • 为了修复最后一个错误,我不得不将 if 语句移到流星方法中。
    猜你喜欢
    • 2016-03-20
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 2014-11-12
    • 2015-09-07
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    相关资源
    最近更新 更多