简体   繁体   中英

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

I have a collection in which I want to only insert data when the Collection.find().count() === 0 . I am using a meteor method to do the insert, so that I can also insert the Meteor userId, my end goal is to run a update function on that particular Collection, which I got working, however the method that does the initial insertion keeps running even though it is wrapper in a if statement that say only do so when the collection is empty.

Everything is published and subscribed, and there is so Financials.allow and and deny code, as wells as a permissions file with ownsDocument. If needed I will post all the code.

Also in the console I am getting this error, despite the fact the the method is inserting the data, even when the collection has a document, which I only want it to insert when it is empty, so a clean install pretty much.

Thanks for any help in advance.

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

Try moving financials.js into your /server directory or wrapping the Meteor.methods code in an "if (Meteor.isServer)" condition to make sure this is running on the server:

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

If you want to run the method on the client, use Meteor.userId() for the value of userId.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM