Best practice to check for last order in Collection

Hi everyone,

I am building an app with React/Meteor and I need to check for the following thing in different components:

the idea basically is to check if the user has already made an order since yesterday 3pm. So I have to:

  • Go to the Order collection
  • Find the user’s last order
  • check whether this order was created after 3pm.
  • return true/false.

So I was thinking: what exactly is the best practice to do so ? Here are the three ideas that I have in mind but I’m a meteor beginner so maybe there are other better options I don’t know.

  • Should I write a method in my api/order.js file and use Meteor.call?
  • Should I publish and subscribe ?
  • or maybe write like a helper an import it in my components ?

Hi there,

I feel the answer differs depending on the size of what you are doing. For a prototype you can easily do pub/sub or method.

“Should I write a method” and “write like a helper and import it” is the same thing. The ‘helper’ would need to call a method or use the pub/sub.

If you want business grade and large number of users and don’t have time to write tests, avoid pub/sub as much as possible, add Redux (I can give you the entire framework but it’s best if you learn it). Only pub/sub the current user.

But the easiest way to do it in your case, if you only need that particular information, do a $set on the user.profile or on the user, something like ‘lastOrder’ and save the date there when you insert an order. This way you don’t need to do an extra query. If you need the id of the last order you can indeed save lastOrder as an object { orderId: …, createdOn… }

Best of success.

1 Like

Hey @paulishca,

Thank you very much for your answer.

So to give you more information, I need to do this for a business app that will have a lot of visitors (I mean just about 5000 visits per month).

I know Redux would fit best but unfortunately I don’t know it yet and won’t have time to learn it before I meet the deadlines.

So I’ll probably go with your solution: store the order in my collection, and also add a hasOrder column in the user profile with the date of the last order. then sub/pub the Meteor.user().hasOrder

1 Like