通过 Meteor 中的 ReactiveVar 传递数组 [英] Passing array via ReactiveVar in Meteor
问题描述
我有一个 Meteor 方法可以返回我的应用程序中的所有用户帐户
I have a Meteor method that returns all user accounts on my application
returnUsers: function(){
return Meteor.users.find().fetch();
}
我正在使用 new ReactiveVar
将 Meteor 方法的返回值传递给我的模板助手:
I'm using new ReactiveVar
to pass the return value of the Meteor method into my template helper:
Template.listViewTemplate.created = function (){
var self = this;
self.myAsyncValue = new ReactiveVar("Waiting for response from serv er...");
Meteor.call('returnUsers', function (err, users) {
if (err)
console.log(err);
else
self.myAsyncValue.set(users);
});
}
Template.listViewTemplate.helpers({
userCollection: function(){
return Template.instance().myAsyncValue.get();
}
});
但是当我将用户渲染到视图中时,我收到一个控制台错误,内容为
But when I go to render the users into the view, I get a console error that reads
{{#each}} 目前只接受数组
{{#each}} currently only accepts arrays
当我在没有 #each
迭代器的情况下渲染时,使用
When I render without the #each
iterator, using
<ul id='usersList'>
{{userCollection}}
</ul>
我网页上的输出准确地反映了用户数 (2),但阅读了
the output on my web-page accurately reflects the number of users (2), but reads
[对象对象],[对象对象]
[object Object],[object Object]
我很确定这里发生了一些奇怪的事情,因为我使用的是全局 Meteor 集合(Meteor.users.find().fetch()
,而不是定义了我自己的收藏),但我不知道如何解决它.
I'm pretty sure that there is some funkiness going on here because I'm using a global Meteor collection (Meteor.users.find().fetch()
, as opposed to having defined my own collection), but I'm not sure how to get around it.
我想显示所有用户的列表,以便当前用户可以单击另一个用户并与他们共享文档——不知道如何解决这个问题.
I want to display a list of all users so the current user can click another user and share a document with them--not sure how to get around this.
推荐答案
您不需要为此使用反应变量.Template.listViewTemplate.created
中的函数不是自动运行中的容器,这意味着:它不会被重新计算.
You don't need to use a reactive variable for this. The function at Template.listViewTemplate.created
is not container in an autorun, which means: It won't get recomputed.
最适合您的场景的方法是:使用一个变量来获取状态(加载、加载、错误)和另一个变量来保存数组本身附加到自身.反应性很酷,但你应该只在需要时使用它.
The best approach for your scenario is: Use a variable to get the status ( loading, loaded, error) and another variable to save the array itself attach to self. Reactivity is cool but you should only use it when needed.
关于:
[对象对象],[对象对象]
[object Object],[object Object]
发生这种情况是因为您没有从提供的对象中提取任何值,也没有使用 {{#each}} 进行循环.
This is happening because you're not extracting any value form the object provided nor looping using {{#each}}.
您列出用户的解决方案既危险又低效.您正在向客户端发送用户集合中的所有字段,包括登录令牌.
Your solutions for listing users is dangerous and inefficient. You're sending to the client all the fields from the user collection, including login tokens.
最好的方法是创建一个订阅,只发送必要的字段,如:_id、info.firstName.您还应该有一些标准来列出用户并使用分页.还可以考虑为此目的使用搜索功能.
The best approach is to create a subscription that send only the necessaries fields like: _id, info.firstName. You should also have some criteria to the list users and use pagination. Consider also a search feature for such purpose.
这篇关于通过 Meteor 中的 ReactiveVar 传递数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!