流星-为什么我应该尽可能在Meteor.userId()上使用this.userId? [英] Meteor - Why should I use this.userId over Meteor.userId() whenever possible?
问题描述
由David Glasser在GitHub问题中的此评论判断:
Judging from this comment by David Glasser in the GitHub issues:
this.userId
是主要API,Meteor .userId()
是JavaScript新用户的语法糖,他们可能不了解尚未成功使用它的细节
this.userId
is the primary API andMeteor.userId()
is syntactic sugar for users new to JavaScript who might not understand the details of successfully using this yet
似乎我们应该尽可能使用 this.userId
(例如在方法函数中,您可以同时使用两者),而只能使用 Meteor.userId()
。如果这个假设是正确的, 为什么 ?
It seems like we should use this.userId
whenever possible (such as inside a method function, where you can use both), and only use Meteor.userId()
inside publish functions. If this assumption is correct, why?
(参考代码的相关位也将有所帮助,我似乎找不到它)
(Referring to the relevant bits of the code would also be helpful, I can't seem to find it)
推荐答案
您的问题似乎将 Meteor.userId( )
和 Meteor.user()
。问题的主体似乎正在询问前者,而主题行正在询问后者。我会尝试同时解决这两个问题。
Your question seems to conflate Meteor.userId()
and Meteor.user()
. The body of the question seems to be asking about the former while the subject line is asking about the latter. I'll try to address both.
- 在服务器上的发布函数中,调用
Meteor.userId ()
或Meteor.user()
将导致错误。而是分别使用this.userId
或Meteor.users.findOne(this.userId)
。但是,请注意,仅当客户端订阅时才调用发布功能。如果要在用户记录更改时更改发布,则需要observe()
由Meteor.users.find( this.userId)
并在记录更改时采取适当的措施。 -
在服务器上,正在处理方法调用的情况下,
Meteor.userId()
和Meteor.user()
分别对应于主叫用户的ID及其记录。但是,请注意,对Meteor.user()
的调用将导致数据库查询,因为它们是基本上等于Meteor.users.findOne(Meteor.userId())
。
- On the server, within a publish function, calling either
Meteor.userId()
orMeteor.user()
will cause an error. Instead, usethis.userId
orMeteor.users.findOne(this.userId)
, respectively. However, note that the publish function is only called when a client subscribes. If you want the publication to change when the user record changes, you'll need toobserve()
the cursor returned byMeteor.users.find(this.userId)
and take appropriate action when the record changes. On the server, while a method call is being processed,
Meteor.userId()
andMeteor.user()
will correspond to the ID of the calling user and their record, respectively. However, be aware that calls toMeteor.user()
will result in a DB query because they are essentially equivalent toMeteor.users.findOne(Meteor.userId())
.
直接在方法调用中,也可以使用 this.userId
代替 Meteor.userId()
,但是您不太可能看到明显的性能差异。当服务器收到方法调用时,它运行您的用户ID(和其他一些信息)中存储在特定插槽在光纤上。 Meteor.userId()
只是从当前光纤的插槽中检索ID。
Directly within a method call, you can also use this.userId
instead of Meteor.userId()
, but you are unlikely to see a significant performance difference. When the server receives the method call, it runs your method implementation with the user's ID (and some other info) stored in a particular slot on the fiber. Meteor.userId()
just retrieves the ID from the slot on the current fiber. That should be fast.
使用 Meteor.userId()
重构代码通常比<$ c更容易。 $ c> this.userId ,因为您不能在方法主体之外使用 this.userId
(例如 this
在您从方法主体调用的函数中将没有'userId'属性),并且您无法在客户端上使用 this.userId
。
It's generally easier to refactor code that uses Meteor.userId()
than this.userId
because you can't use this.userId
outside of the method body (e.g. this
won't have a 'userId' property within a function you call from the method body) and you can't use this.userId
on the client.
这篇关于流星-为什么我应该尽可能在Meteor.userId()上使用this.userId?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!