流星-为什么我应该尽可能在Meteor.userId()上使用this.userId? [英] Meteor - Why should I use this.userId over Meteor.userId() whenever possible?

查看:121
本文介绍了流星-为什么我应该尽可能在Meteor.userId()上使用this.userId?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由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 and Meteor.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.


  1. 在服务器上的发布函数中,调用 Meteor.userId () Meteor.user()导致错误。而是分别使用 this.userId Meteor.users.findOne(this.userId)。但是,请注意,仅当客户端订阅时才调用发布功能。如果要在用户记录更改时更改发布,则需要 observe() Meteor.users.find( this.userId)并在记录更改时采取适当的措施。

  2. 在服务器上,正在处理方法调用的情况下, Meteor.userId() Meteor.user()分别对应于主叫用户的ID及其记录。但是,请注意,对 Meteor.user()的调用将导致数据库查询,因为它们是基本上等于 Meteor.users.findOne(Meteor.userId())

  1. On the server, within a publish function, calling either Meteor.userId() or Meteor.user() will cause an error. Instead, use this.userId or Meteor.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 to observe() the cursor returned by Meteor.users.find(this.userId) and take appropriate action when the record changes.
  2. On the server, while a method call is being processed, Meteor.userId() and Meteor.user() will correspond to the ID of the calling user and their record, respectively. However, be aware that calls to Meteor.user() will result in a DB query because they are essentially equivalent to Meteor.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆