簡體   English   中英

流星方法當collection.find()。count()=== 0時插入集合

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

我有一個我只想在Collection.find().count() === 0時插入數據的Collection.find().count() === 0 我正在使用流星方法進行插入,因此我也可以插入流星userId,我的最終目標是在我工作的那個特定Collection上運行更新功能,但是進行初始插入的方法仍在運行即使它是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
    };
  }
});

嘗試將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的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM