【发布时间】:2015-05-03 16:24:03
【问题描述】:
提交帖子后,当前用户似乎无法识别,并且用户名显示为空白。提交的 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};
},
Discover Meteor 教程中也没有其他对 author 的引用,而且它确实有效。但是我的不起作用。这个问题似乎是在我添加 UserAccounts 包之后开始的。或者可能是文件夹位置问题?
更新 [2015 年 5 月 11 日]
我意识到,当使用 Meteor 附带的原始 ui 帐户时,它具有 username 字段,因为 {{> login}} 附带的注册链接使用用户名 + 密码。不涉及电子邮件地址。
另一方面,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 下(服务器端)
【问题讨论】:
-
不要包含图片,请尝试包含代码(可能还有用于响应的 cmets)。当您直接在控制台中执行
{{expr}}时,花括号无法进行对象文字解释,因此改为代码块。这意味着它几乎与expr完全相同。即您尝试在 Console 中访问的author使用的是普通 JavaScript 而不是流星。 -
@PaulS。我的错。让我删除
{{expr}}上的部分。至于代码,由于 devtool 没有抛出错误,我不确定要考虑粘贴代码的哪一部分。我对所有具有{{author}}或user的部分进行了搜索,因此只粘贴了集合中的部分。您对从哪里开始寻找或使用其他工具等有什么建议吗?
标签: javascript meteor