0

我有一个集合,我只想在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/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
    };
  }
});
4

1 回答 1

0

尝试将 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 的值。

于 2015-05-14T20:03:33.130 回答