我将用户数据存储在 Meteor.users.profile
中,但在通过 return 命令检索它们时遇到问题。
代码如下所示:
Template.details.events({
'submit form': function(event) {
event.preventDefault();
var currentUser = Meteor.userId();
var name = event.target.nam.value;
var age = event.target.nombor.value;
// var gender = event.target.sex.value;
// var gen = Meteor.user().profile.gender;
Meteor.users.update({
_id: currentUser
}, {
$set: {
"profile.name": name,
"profile.age": age
}
});
Router.go('/tryy');
}
});
Template.tryy.helpers({
'people': function() {
//var gender = Meteor.user().profile.gender;
return Meteor.users.find({}, {
gender: "gender"
});
}
});
HTML:
<template name="tryy">
<ul>
{{#each people}}
<li><a href="#"> {{name}} {{age}} </a></li>
{{/each}}
</ul>
</template>
这有什么问题:return Meteor.users.find({}, {gender: "gender"});
?
我想查看与currentUser
相反的性别列表。
最佳答案
嗯……我已经回答你的另一个问题了
首先,你应该处理你的助手中没有登录用户的情况:
Template.tryy.helpers({
'people': function() {
// you should deal with the situation when no current user
if (Meteor.user() === null) return null; // or others way you like
var gender = Meteor.user().profile.gender;
return Meteor.users.find({}, {
gender: "gender"
});
}
});
我认为你应该阅读Meteor document of Mongo collection在问这些问题之前和 Mongo 文档...
正如你所问:
要查找名称与对象的属性(profile.name)匹配的用户:
Meteor.users.find({"profile.gender": 性别});
其他一些有用的简单查询:
要查找姓名出现在数组中的用户(假设名为friends):
Meteor.users.find({"profile.name":friends});
如果我们有 Alice、Bob、Cathy, friend 是 ['Alice', 'Cathy'],它将返回 Alice 和 Cathy
总而言之,你应该仔细阅读这些文档,它会对你有很大帮助:-)
关于javascript - 如何在 Meteor 中指定字段说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32176472/