Meteor.methods and check()


#1

I have a couple of questions about the relationship between Meteor.call and the check package. Hopefully this isn’t too cumbersome. I am still learning and would love some pointers.

  1. Is it required to check all arguments within a Meteor.methods function? Currently I get this error on the server when not all arguments are being checked. I’m just not sure if this is a newer requirement and I don’t see anything in the docs about it being required. Although, clearly you want to check everything (I get that).

    Error: Did not check() all arguments during call to...

  2. What is the best way to check() an object created from a Collection.find({})? This seems to be where I am getting hung up the most. The following results in this error on the server Error: Match error: Expected plain object

     var subscriptions = CartSubscriptions.find();
     var customer = {
         name: "Matt"
     };
    
     Meteor.call('createNewCustomer', customer, subscriptions, function(error, response) {...});
    
     Meteor.methods({
         createNewCustomer: function(customer, subscriptions, singles, line_items) {
         check(customer, {
             name: String,
         });
         check(subscriptions, {});  // I don't know how to check this
     });
    

Should I use CartSubscriptions.find().fetch(); instead? I have tried both ways and neither seem to be doing the trick.

Thanks for the help and if any one has an article or anything relevant to this please let me know.


#2

I guess one other thought I had was I could call the subscriptions collection on the server within the method there instead. Although I am not sure what the best practice. And that doesn’t really solve my question either.

EDIT: This won’t work because the collections I am working with are local collections.


#3

Collection.find({}) returns a mongo cursor, so if it’s a collection that the user has subscribed to then the server already has full access to the collection and you can use the userId of the client calling the method if you need. If it’s a local collection, then I don’t think passing the cursor to the server will be at all useful.

What is it you’re trying to accomplish by passing the cursor/collection to the server as a part of the method?


#4

Ah, right that makes total sense and something I discovered since my last post. So I am sending some local collections to the server. Basically this is just a shopping cart. I am putting products into a couple local collection (using jeffm:local-persist to persist the data from page to page). Then on checkout submit pushing those collections to the server to ping Stripe and finish up the orders.

My goal is to make this as simple as possible, and I feel like I am overcomplicating the issue. Especially after reading your reply. I guess I am just not sure of the best way to hold the “cart” data before someone wants to checkout. I have thought about using Sessions or maybe a collection on the server. But I dont really want to require someone to sign up for an account before they checkout.


#5

That makes sense, I thought it might be a local collection. In that case, Collection.find().fetch() will return an array, even if there is only one document, as I recall. So you could check it is an array and check that each object in it has the required attributes.


#6

Any idea how that should look? I’ve got it passing through fine now with…

var subscriptions = CartSubscriptions.find().fetch()

check(subscriptions, Match.Any);

But I am not sure how to format it to test the subscriptions Array with both the Match.Any for the array with something more specific for the objects within the array.


#7

I believe you can use Match.ObjectIncluding({}) which will let you check for keys and the types of those keys, but I’m not 100% sure and haven’t tried it out.

http://docs.meteor.com/#/full/matchpatterns