How can I prevent a temporary hole Meteor.user ()?

Template request ...

if (this.userRequesting._id === Meteor.user()._id) { ... } 

... verify the identity of the person. (I suspect this is not the best way to handle this, but I will save this for another question so as not to overload this question).

My problem is that this leads to a huge cascade of errors, assuming that Meteor.user() is undefined, only on pageload ... After loading the page, this code runs without problems. Clearly, there is a temporary hole (or, as I believe) where Meteor.user() remains unassigned for a split second when the page loads. Replacing the above code with ...

  ____________ if (Meteor.user() && this.userRequesting._id == Meteor.user()._id) { ... } 

... fixes the problem, but ... Come on ... It can't be kosher. What assumption am I mistaken about patterns and the Meteor?

If appropriate, I use accounts-password as my account manager.


[Edit]: This seems to be the same problem , but I leave this question on the ground that I am looking for a fix, not a workaround. (I already know that checking Meteor.user() resolves this.)

+1
source share
2 answers

Yes, you basically get it. When you first refresh the page, the user is not considered registered, and he must make sure that he is. But the template material is called immediately, and then again when another material is loaded (if you put alert in the rendered method, you will notice this). The best solution is exactly what you suggested, although you can use Meteor.userId() to get just the identifier:

 if (Meteor.userId() && Meteor.userId() === this.userRequesting._id) { // ... 
+1
source

You can use Meteor.userId() to get the _id attribute for the signed-in user, or null (I think) if the user has not logged in.

0
source

Source: https://habr.com/ru/post/1201255/


All Articles