Meteor.loginWithPassword回调不';t在用户帐户文档中提供自定义对象
Meteors loginWithPassword()函数不提供对象systemData,我在注册期间将其添加到用户文档(而不是配置obj)。问题是,如果我在登录后查看控制台,我可以看到该对象systemData(这意味着它可能不是发布问题),但不在loginWithPassword()函数的回调中,我需要它们(将用户动态重定向到正确的页面)。有没有办法得到这个物体,而不需要像计时器这样丑陋的东西Meteor.loginWithPassword回调不';t在用户帐户文档中提供自定义对象,login,meteor,Login,Meteor,Meteors loginWithPassword()函数不提供对象systemData,我在注册期间将其添加到用户文档(而不是配置obj)。问题是,如果我在登录后查看控制台,我可以看到该对象systemData(这意味着它可能不是发布问题),但不在loginWithPassword()函数的回调中,我需要它们(将用户动态重定向到正确的页面)。有没有办法得到这个物体,而不需要像计时器这样丑陋的东西 Meteor.loginWithPassword(email, password, functio
Meteor.loginWithPassword(email, password, function(errorObject) {
if (errorObject) {
...
} else {
// returns true
if (Meteor.userId()) {
// returns false
if (Meteor.user().systemData) {
...
}
// user doc without systemData object
console.log(JSON.stringify(Meteor.user());
}
}
我添加了有关创建用户的对象系统数据:
Accounts.onCreateUser(function(options, user) {
if (options.profile) {
user.profile = options.profile;
}
...
user.systemData = systemDataRegularUser;
return user;
});
您确定要将数据发布到客户端吗 我在回调函数中使用loginWithPassword获取用户信息
Meteor.loginWithPassword username,password,(error,result1)->
options =
username: username
password: password
email: result['data']['email']
profile:
name: result['data']['display-name']
roles: result.roles
console.log Meteor.user(), result1
我创建用户流代码:(选项包含systemData)
第一个问题是,您希望在发布到客户端的用户文档上有一个自定义字段。这是一个常见的问题-请参阅答案 下一个问题是,即使您添加了以下内容:
Meteor.publish("userData", function () {
return Meteor.users.find(this.userId, {fields: {systemData: 1}});
});
我想你还有比赛条件。当您使用密码调用loginWithPassword
时,服务器将发布您的用户文档,但它也将使用systemData
字段发布同一文档的另一个版本。您希望在调用Meteor.user()
时这两个事件都已完成。在实践中,这可能只是工作,但我不确定是否有任何保证,它总是会。正如你所建议的,如果你在计时器上加了一点延迟,那么它可能会工作,但这是一个丑陋的黑客行为
或者,您可以将
系统数据
添加到用户的配置文件
中,以便始终发布它吗?我没有找到解决此问题的确切方法,但找到了简单的解决方法。
要在用户登录后立即执行某些操作(例如,动态地将用户重定向到正确的页面),您可以使用Iron router挂接您的主页(如果您使用它的话):
单击注销后当然if(isCurrentUserAdmin()){Session.set('adminJustLogged',null);}
我进一步考虑了调用Meteor.call('someMethod')
在方法回调中获取userData对象,但现在我很满意了
PS:我知道不建议使用大量会话变量或其他反应式数据源来加速我的应用程序,但我相信,这不是悲剧:)
PS2:不管怎样,谢谢你的回答。为什么我没有在我在中描述的配置文件对象中包含我的机密对象systemData。Meteor帐户包中的发布函数可能受到一些干扰:
Meteor.publish(null,function(){})代码>其中是硬编码的{profile:1,username:1,email:1}
。我试着在我的应用程序中扩展它,但没有成功。
Meteor.publish("userData", function () {
return Meteor.users.find(this.userId, {fields: {systemData: 1}});
});
this.route('UsersListingHome', {
path: '/',
template: 'UsersListing',
data: function() { ... },
before: function() {
if (isCurrentUserAdmin() && Session.get('adminJustLogged') !== 'loggedIn') {
Router.go('/page-to-redirect');
Session.set('adminJustLogged','loggedIn');
}
}
});