How to detect an operation by system or Meteor it self?



I’m figuring out how to detect an operation e.g. “insert” by system or by Meteor it self, not by user via browser.

    insertCustomer: function(post) {
        if (!this.userId) {
            throw new Meteor.Error('customers.insertCustomer.notLoggedIn',
                'Must be logged in.');

If i call that method above from e.g startup.js:

const customers = [
        firstname: 'John',
        lastname: 'Doe',
        firstname: 'Merry',
        lastname: 'Anne',

users.forEach((customer) => {'insertCustomer', customer);

It will return an error “Must be logged in.” because Meteor.userId will be null if i call that method from startup.js

Q: My question is how to run “insertCustomer” method with condition “if user is loged in” or “it is called by system or Meteor it self”?

Thanks for some help on this


I would solve this with the following package:

The following excerpt from that page:

It is quite normal for userId to sometimes be unavailable to hook callbacks in some circumstances. For example, if an update is fired from the server with no user context, the server certainly won’t be able to provide any particular userId.

Leads me to believe you can check the userId value in a before.insert hook and if it’s null it’s from the server otherwise a user initiated the call.

Customers.before.insert(function (userId, doc) { if(userId) { // Do user thing } });

I believe Meteor methods are not meant to be called form the server, you can just directly Cusomers.insert(document) and let the before.insert do the user\system logic. I think it’s odd you are still inserting the document even though the user isn’t logged in though.