Javascript 发布和订阅无法正常工作
发布和订阅不起作用。请在下面找到答案 初始问题: 我正在尝试发布facebook first_名称,该名称在使用Meteor中的帐户facebook包登录时自动检索(存储在services.facebook下的用户集合中)。我已经自动发布和不安全删除 到目前为止,我所尝试的是: 服务器端 客户端 我在模板中使用的是以下内容Javascript 发布和订阅无法正常工作,javascript,facebook,meteor,publish-subscribe,meteor-accounts,Javascript,Facebook,Meteor,Publish Subscribe,Meteor Accounts,发布和订阅不起作用。请在下面找到答案 初始问题: 我正在尝试发布facebook first_名称,该名称在使用Meteor中的帐户facebook包登录时自动检索(存储在services.facebook下的用户集合中)。我已经自动发布和不安全删除 到目前为止,我所尝试的是: 服务器端 客户端 我在模板中使用的是以下内容 <div class="Name"><p>{{currentUser.services.facebook.first_name}}</p>
<div class="Name"><p>{{currentUser.services.facebook.first_name}}</p></div>
{{currentUser.services.facebook.first_name}
在删除autopublish之前,该名称会显示在模板中。当用户通过facebook oauth API登录并且使用Faceomer帐户facebook实现身份验证时,所有需要的数据都会存储在当前用户对象(meteor.user())中 因此,您案例中的用户模式类似于以下内容:
{
"_id": "Ap85ac4r6Xe3paeAh",
"createdAt": "2015-12-10T22:29:46.854Z",
"services": {
"facebook": {
"accessToken": "XXX",
"expiresAt": 1454970581716,
"id": "XXX",
"email": "ada@lovelace.com",
"name": "Ada Lovelace",
"first_name": "Ada",
"last_name": "Lovelace",
"link": "https://www.facebook.com/app_scoped_user_id/XXX/",
"gender": "female",
"locale": "en_US",
"age_range": {
"min": 21
}
},
"resume": {
"loginTokens": [
{
"when": "2015-12-10T22:29:46.858Z",
"hashedToken": "XXX"
}
]
}
},
"profile": {
"name": "Sashko Stubailo"
}
}
因此,如果您想要检索一个用户的名称,您所需要做的就是将当前用户发布到客户端,然后从用户对象获取用户名
// server
Meteor.publish("userData", function () {
return Meteor.users.find({_id: this.userId});
});
// client
Meteor.subscribe("userData");
Template.templateName.helpers({
// this function returns username
Username : function(){
// if user is logged in using facebook; otherwise user is logged in using password
if (Meteor.user().profile.name)
return Meteor.user().profile.name;
else
return Meteor.user().username;
}
现在,您可以在视图中显示用户的名称:{{Username}
以下是……找到问题的解决方案:
在客户端/main.js中设置meteor项目时,它将显示
import./main.html'代码>如果您使用的是路由和模板,而不是main.html模板,这将阻止发布和订阅正常工作。您是否尝试过{fields:{'services.facebook.first_name':1}?是的,我试过了,没有改变任何东西。你的订阅在哪里?你能显示代码吗?这是我现在拥有的第一个订阅。到目前为止,它在client/main.js中。嘿,谢谢你的回答!我试过这个发布功能,但是它不会向客户端返回任何facebook数据。当我在控制台中查找用户对象时,它只显示m我也希望只发布facebook信息的某些数据字段,而不是整个用户。
{
"_id": "Ap85ac4r6Xe3paeAh",
"createdAt": "2015-12-10T22:29:46.854Z",
"services": {
"facebook": {
"accessToken": "XXX",
"expiresAt": 1454970581716,
"id": "XXX",
"email": "ada@lovelace.com",
"name": "Ada Lovelace",
"first_name": "Ada",
"last_name": "Lovelace",
"link": "https://www.facebook.com/app_scoped_user_id/XXX/",
"gender": "female",
"locale": "en_US",
"age_range": {
"min": 21
}
},
"resume": {
"loginTokens": [
{
"when": "2015-12-10T22:29:46.858Z",
"hashedToken": "XXX"
}
]
}
},
"profile": {
"name": "Sashko Stubailo"
}
}
// server
Meteor.publish("userData", function () {
return Meteor.users.find({_id: this.userId});
});
// client
Meteor.subscribe("userData");
Template.templateName.helpers({
// this function returns username
Username : function(){
// if user is logged in using facebook; otherwise user is logged in using password
if (Meteor.user().profile.name)
return Meteor.user().profile.name;
else
return Meteor.user().username;
}