[英]Meteor - missing username Meteor.users.findOne(); or Meteor.users();
提交帖子后,当前用户似乎无法被识别,用户名显示为空白。 提交的html具有{{author}}
来显示撰写帖子的用户。
当我在控制台中键入以下命令时,结果如下:
1) user.username
=>结果user
未定义。
2) Meteor.users.find();
=> LocalCollection.Cursor {collection: LocalCollection, sorter: null, _selectorId: undefined, matcher: Minimongo.Matcher, skip: undefined…}
3) Meteor.users.findOne();
=> Object {_id: "P3ocCTTdvi2o3JApf", profile: Object}
与工作版本相比(请注意,我的版本缺少用户名)
=> Object {_id: "XHXPebzjg5LM5tNAu", profile: Object, username: "Bruno"}
在post.js集合中(在lib文件夹中-与客户端和服务器共享),我有:
Meteor.methods({
postInsert: function(postAttributes) {
check(this.userId, String); //check(Meteor.userId(), String);
check(postAttributes, {
title: String,
message: String
});
var user = Meteor.user();
var post = _.extend(postAttributes, {
userId: user._id,
author: user.username
});
var postId = Posts.insert(post);
return {_id: postId};
},
在“发现流星”教程中也没有其他对author
引用,并且可以正常工作。 但是我的行不通。 添加UserAccounts程序包后,似乎已经开始出现此问题。 还是文件夹位置问题?
更新[2015年5月11日]
我意识到使用流星随附的原始ui帐户时,它具有username
段,因为{{> login}}
随附的注册链接使用用户名+密码。 没有涉及的电子邮件地址。
另一方面,UserAccounts没有此用户名字段。 它是电子邮件/密码或社交登录。 因此,也许有人指导我如何从电子邮件/密码和社交网络按钮登录/登录中获取用户名(作为独立字段或从电子邮件派生)? 然后生病从那里弄弄。
UserAccounts的代码
router.js
//Iron router plugin to ensure user is signed in
AccountsTemplates.configureRoute('ensureSignedIn', {
template: 'atTemplate', //template shown if user is not signed in
layoutTemplate: 'atLayout' //template for login, registration, etc
});
Router.plugin('ensureSignedIn', { //Don't require user logged in for these routes
except: ['login', 'register'] //can use only: too
});
AccountsTemplates.configureRoute('signIn', { //login
name: 'login',
path: '/login',
template: 'atTemplate',
layoutTemplate: 'atLayout',
redirect: '/'
});
AccountsTemplates.configureRoute('signUp', { //registration
name: 'register',
path: '/register',
template: 'atTemplate',
layoutTemplate: 'atLayout',
redirect: '/'
});
并在config.js下(服务器端)
在“用户帐户”文档中,尝试以下操作:
if (Meteor.isServer){
Meteor.methods({
"userExists": function(username){
return !!Meteor.users.findOne({username: username});
},
});
}
AccountsTemplates.addField({
_id: 'username',
type: 'text',
required: true,
func: function(value){
if (Meteor.isClient) {
console.log("Validating username...");
var self = this;
Meteor.call("userExists", value, function(err, userExists){
if (!userExists)
self.setSuccess();
else
self.setError(userExists);
self.setValidating(false);
});
return;
}
// Server
return Meteor.call("userExists", value);
},
});
这会将用户名字段添加到您的登录/注册表单,并在注册新用户时检查用户名冲突。
顶部是服务器代码,底部是通用代码,因此您可以将整个代码段放置在project / lib /文件夹中,或者将方法与其余方法一起放置,具体取决于您构造项目的方式。
使用以下命令使用户名字段在注册表单上首先显示,而不是最后显示:
var pwd = AccountsTemplates.removeField('password');
AccountsTemplates.removeField('email');
AccountsTemplates.addFields([
{
_id: "username",
type: "text",
displayName: "username",
required: true,
func: function(value){
if (Meteor.isClient) {
console.log("Validating username...");
var self = this;
Meteor.call("userExists", value, function(err, userExists){
if (!userExists)
self.setSuccess();
else
self.setError(userExists);
self.setValidating(false);
});
return;
}
// Server
return Meteor.call("userExists", value);
},
minLength: 5,
},
{
_id: 'email',
type: 'email',
required: true,
displayName: "email",
re: /.+@(.+){2,}\.(.+){2,}/,
errStr: 'Invalid email',
},
pwd
]);
https://github.com/meteor-useraccounts/core/blob/master/Guide.md
您需要使用.
访问对象的属性,而不是空间
Meteor.users.findOne
您提供的信息很少,无法发现问题。 这就是说,您如何让用户进入?
请注意,OAuth注册不会在创建的用户对象上提供username
段,也不会仅基于电子邮件地址提供密码注册。 在这些情况下, user.username
将是undefined
。
我建议确保您所有的用户都获得一个profile.username
(可能利用一些Accounts.onLogin
挂钩或某个配置文件页面来强迫用户选择用户名。
之后,您将扩展您的帖子元数据设置author: user.profile.username
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.