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


i have collection in want insert data when collection.find().count() === 0. using meteor method insert, can insert meteor userid, end goal run update function on particular collection, got working, method initial insertion keeps running though wrapper in if statement when collection empty.

everything published , subscribed, , there financials.allow , and deny code, wells permissions file ownsdocument. if needed post code.

also in console getting error, despite fact the method inserting data, when collection has document, want insert when empty, clean install pretty much.

thanks in advance.

exception while simulating 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     @ meteor.methods.financialuserid (http://localhost:3000/lib/collections/financials.js?0fde44b180e856bc334a164ad9859e394fd9578d:22:19)     @ http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:4269:25     @ _.extend.withvalue (http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:955:17)     @ _.extend.apply (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:4260:54)     @ _.extend.call (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:4138:17)     @ http://localhost:3000/client/templates/editable/client_edit.js?578e3f500b0c73d3d22e659540d653cb6309cd52:9:8     @ 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 /server directory or wrapping meteor.methods code in "if (meteor.isserver)" condition make sure running on 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 want run method on client, use meteor.userid() value of userid.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -