【发布时间】:2017-07-31 20:47:36
【问题描述】:
我有一个 Meteor 方法可以返回我的应用程序上的所有用户帐户
returnUsers: function(){
return Meteor.users.find().fetch();
}
我正在使用new ReactiveVar 将 Meteor 方法的返回值传递给我的模板助手:
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();
}
});
但是当我将用户呈现到视图中时,我收到一个控制台错误,内容为
{{#each}} 目前只接受数组
当我在没有 #each 迭代器的情况下渲染时,使用
<ul id='usersList'>
{{userCollection}}
</ul>
我的网页上的输出准确地反映了用户数量 (2),但读取
[对象对象],[对象对象]
我很确定这里发生了一些有趣的事情,因为我使用的是全局 Meteor 集合(Meteor.users.find().fetch(),而不是定义我自己的集合),但我不知道如何解决它。
我想显示所有用户的列表,以便当前用户可以单击另一个用户并与他们共享文档——不知道如何解决这个问题。
【问题讨论】:
-
尝试使用空数组而不是字符串来初始化反应变量。我不知道这是不是唯一的问题,但这肯定是个问题。
-
如
self.myAsyncValue = new ReactiveVar([]);? -
似乎没有产生任何影响
-
console.log(typeof Template.instance().myAsyncValue.get());告诉我他们是类型object -
这可能吗?通过文档
A ReactiveVar holds a single value that can be get and set
标签: javascript meteor