如何在 Meteor 中指定字段说明符
How to specify field specifier in Meteor
我将用户数据存储在 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
相反性别的列表。
find()
的第一个参数是选择器,第二个参数是其他选项。在这种情况下,如果您想按性别查找用户,您可以使用选择器:
Meteor.users.find({"profile.gender": gender});
这将 return 所有具有指定 gender
的用户。也请看看非常详细的Meteor documentation.
此外,在您的模板中,您应该将 name
更改为 profile.name
(对于年龄也是如此),因为这是您存储这些值的地方:
{{#each people}}
<li><a href="#"> {{profile.name}} {{profile.age}} </a></li>
{{/each}}
嗯嗯……我已经回答你的另一个问题了
首先,你应该处理你的助手中没有登录用户的情况:
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": gender});
一些其他有用的简单查询:
查找名称出现在 数组中的用户(假设它名为 friends):
Meteor.users.find({"profile.name": friends});
如果我们有 Alice、Bob、Cathy 和朋友是 ['Alice'、'Cathy'],它将 return Alice 和 Cathy
总而言之,您应该仔细阅读这些文档,它会对您有很大帮助:-)
我将用户数据存储在 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
相反性别的列表。
find()
的第一个参数是选择器,第二个参数是其他选项。在这种情况下,如果您想按性别查找用户,您可以使用选择器:
Meteor.users.find({"profile.gender": gender});
这将 return 所有具有指定 gender
的用户。也请看看非常详细的Meteor documentation.
此外,在您的模板中,您应该将 name
更改为 profile.name
(对于年龄也是如此),因为这是您存储这些值的地方:
{{#each people}}
<li><a href="#"> {{profile.name}} {{profile.age}} </a></li>
{{/each}}
嗯嗯……我已经回答你的另一个问题了
首先,你应该处理你的助手中没有登录用户的情况:
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": gender});
一些其他有用的简单查询:
查找名称出现在 数组中的用户(假设它名为 friends):
Meteor.users.find({"profile.name": friends});
如果我们有 Alice、Bob、Cathy 和朋友是 ['Alice'、'Cathy'],它将 return Alice 和 Cathy
总而言之,您应该仔细阅读这些文档,它会对您有很大帮助:-)