Meteor 方法在 collection.find().count() === 0 时插入集合

Meteor Method Insert into collection when collection.find().count() === 0

我有一个集合,我只想在 Collection.find().count() === 0 时插入数据。我正在使用 meteor 方法进行插入,这样我也可以插入 Meteor userId,我的最终目标是 运行 我开始工作的那个特定集合的更新函数,但是执行的方法初始插入保持 运行ning,即使它是 if 语句的包装器,表示仅在集合为空时才这样做。

一切都已发布和订阅,因此有 Financials.allow 和拒绝代码,以及带有 ownsDocument 的权限文件。如果需要,我将 post 所有代码。

我也在控制台中收到此错误,尽管事实上该方法正在插入数据,即使集合有一个文档,我只希望它在它为空时插入,所以全新安装差不多。

提前感谢您的帮助。

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

尝试将 financials.js 移动到您的 /server 目录或将 Meteor.methods 代码包装在 "if (Meteor.isServer)" 条件中以确保这是 运行ning 在服务器上:

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的值。