我正在尝试使用 Metor.methods 获取 meteor 中所有用户的列表
这是我的代码:
服务器/main.js
Meteor.methods({
'createUser': function(){
if (Meteor.users.find({}).count()===0) {
for (i = 0; i <= 5; i++){
let id = Accounts.createUser({
email: Meteor.settings.ADMIN_USE,
password: Meteor.settings.ADMIN_PASSWORD,
profile: { firstName: Meteor.settings.ADMIN_FIRSTNAME, lastName: Meteor.settings.ADMIN_LASTNAME }
});
}
}
},
'returnmail': function(){
return Meteor.users.findOne().emails[0].address;
}
});
然后我在另一个名为 的文件中调用这个函数Listusers.js :
Template.ListUsers.helpers({
email: function(){
Meteor.call('returnmail');
},
});
我正在尝试使用此代码显示电子邮件的值,但它不起作用
客户端/ListUsers.html
<Template name="ListUsers">
<input id="mail" type="text" value="{{email}}" />
</Template>
最佳答案
几个问题。我强烈建议您通过 the tutorial至少。 Discover Meteor电子书也是无价之宝。理解 Meteor 的第一步是从传统的 XHR 请求-响应模型转变为发布-订阅模型。
email
helper 需要return
一个值。 Meteor.call()
不返回任何东西。通常,您将它与一个回调一起使用,该回调为您提供错误状态和结果。但是,除非您使用 Session 变量或 promise,否则您不能在 helper 中使用它,因为调用的返回值位于错误的上下文级别。 returnmail
方法仅返回来自 findOne()
的单个电子邮件地址也不是任何特定的,只是一个准随机的(你不能保证哪个文档 findOne()
会返回!)现在进入解决方案。
服务器:
Meteor.publish('allEmails',function(){
// you should restrict this publication to only be available to admin users
return Meteor.users.find({},{fields: { emails: 1 }});
});
客户端js:
Meteor.subscribe('allEmails');
Template.ListUsers.helpers({
allUsers(){ return Meteor.users.find({}); },
email(){ return this.emails[0].address; }
});
客户端html:
<Template name="ListUsers">
{{#each allUsers}}
<input id="mail" type="text" value="{{email}}" />
{{/each}}
</Template>
关于meteor - 在 Meteor 中显示用户电子邮件地址列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37469191/