简体   繁体   English

Accounts.onLogin如何获取用户ID?

[英]Accounts.onLogin how to get user Id?

How do you get the _id of the user that logged in. I have tried the following combinations and I get errors, or undefined 你如何获得登录用户的_id。我尝试了以下组合,我得到错误或未定义

Upon user creation, the user is automatically signed into the application. 在用户创建时,用户自动登录到应用程序。 Is the user that is returned by the Accounts.onCreateUser function occurring after the user is logged in? userreturnedAccounts.onCreateUser用户后发生功能登录?

Accounts.onLogin(function(){

 var user = this.userId / Meteor.user() / Meteor.user()._id 
 console.log(user)

})

http://docs.meteor.com/#/full/accounts_onlogin http://docs.meteor.com/#/full/accounts_onlogin

The Accounts.onLogin(function(){}) , come with 1 parameter user. Accounts.onLogin(function(){})带有1个参数用户。

When it is known which user was attempting to login, the Meteor user object. 当知道哪个用户尝试登录时,Meteor用户对象。 This will always be present for successful logins. 这将始终存在成功登录。

from the docs . 来自文档

So this should work. 所以这应该工作。

Accounts.onLogin(function(user){
  console.log(user.user._id)
});

And you should see, all the user document into the server console,check this meteorpad for an example. 您应该看到,所有用户文档都进入服务器控制台,请查看此meteorpad以获取示例。

NOTE: this feature is only available on server side check this Hooks Accounts.onLogin/onLoginFailure should be available on client 注意:此功能仅在服务器端可用,请检查此Hooks Accounts.onLogin / onLoginFailure应在客户端上可用

You can always get the _id of the logged-in user via Meteor.userId() . 您始终可以通过Meteor.userId()获取已登录用户的_id。 This also works inside the Accounts.onLogin callback 这也适用于Accounts.onLogin回调

Accounts.onLogin(function() {
  console.log(Meteor.userId());
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM