用#each 迭代 Meteor 游标会出错
Iterating Meteor cursor with #each gives error
我有以下代码并且正在获取
{{#each}} currently only accepts arrays, cursors or falsey values
HTML:
<template name='subscribers'>
{{#each stat in getPythonStats "WNScan"}}
<div>
{{stat.name}}: {{stat.value}}
</div>
{{/each}}
</template>
Javascript:
Template.subscribers.helpers({
getPythonStats(server) {
var status = WNstatus.find({'server': server});
if (!status)
status = [{'status': 'shutdown'}, {'count': 0}, {'startTime': 'Never'}, {'runTime': 0}];
console.log(status);
return status;
}
});
控制台输出:
{_id: M…D.ObjectID, server: "WNScan", status: "shutdown", startTime: "2017-10-10 22:40:12", runTime: 5.39, …}
在我看来就像一个光标。一开始我以为问题出在 getPythonStats
的参数上,混淆了 #each
,但这似乎不是问题,因为正在调用 helper。我已经尝试了 collection.find
和 collection.findOne
(return 相同的结果,因为满足查询的文档永远不会超过一个),结果相同。我在其他地方有几乎相同的代码使用 {{with getPythonStats "WNScan"}}
并且工作正常。此外,如果未找到 "WNScan"
文档,则 returned 数组会产生相同的错误。这是最新的 1.5 版本的 Meteor。
据我了解,您想遍历 status
中的每个键,但是 #each 需要一个数组或游标 而 status
则不需要!
collection.find
returns 游标,collection.findOne
returns 文档(不是文档数组)。
collection.findOne
使用 collection.find
的等价物是 collection.find().fetch()[0]
Javascript:
const defaultStatus = {
status: 'shutdown',
count: 0,
startTime: 'Never',
runTime: 0
};
Template.subscribers.helpers({
getPythonStats(server) {
const status = WNstatus.findOne({ 'server': server});
console.log(status);
return _.pairs(status || defaultStatus);
}
});
HTML
<template name='subscribers'>
{{#each stat in getPythonStats "WNScan"}}
<div>
<!-- stat is now an array containing key and value -->
{{stat.[0]}}: {{stat.[1]}}
</div>
{{/each}}
</template>
我有以下代码并且正在获取
{{#each}} currently only accepts arrays, cursors or falsey values
HTML:
<template name='subscribers'>
{{#each stat in getPythonStats "WNScan"}}
<div>
{{stat.name}}: {{stat.value}}
</div>
{{/each}}
</template>
Javascript:
Template.subscribers.helpers({
getPythonStats(server) {
var status = WNstatus.find({'server': server});
if (!status)
status = [{'status': 'shutdown'}, {'count': 0}, {'startTime': 'Never'}, {'runTime': 0}];
console.log(status);
return status;
}
});
控制台输出:
{_id: M…D.ObjectID, server: "WNScan", status: "shutdown", startTime: "2017-10-10 22:40:12", runTime: 5.39, …}
在我看来就像一个光标。一开始我以为问题出在 getPythonStats
的参数上,混淆了 #each
,但这似乎不是问题,因为正在调用 helper。我已经尝试了 collection.find
和 collection.findOne
(return 相同的结果,因为满足查询的文档永远不会超过一个),结果相同。我在其他地方有几乎相同的代码使用 {{with getPythonStats "WNScan"}}
并且工作正常。此外,如果未找到 "WNScan"
文档,则 returned 数组会产生相同的错误。这是最新的 1.5 版本的 Meteor。
据我了解,您想遍历 status
中的每个键,但是 #each 需要一个数组或游标 而 status
则不需要!
collection.find
returns 游标,collection.findOne
returns 文档(不是文档数组)。
collection.findOne
使用 collection.find
的等价物是 collection.find().fetch()[0]
Javascript:
const defaultStatus = {
status: 'shutdown',
count: 0,
startTime: 'Never',
runTime: 0
};
Template.subscribers.helpers({
getPythonStats(server) {
const status = WNstatus.findOne({ 'server': server});
console.log(status);
return _.pairs(status || defaultStatus);
}
});
HTML
<template name='subscribers'>
{{#each stat in getPythonStats "WNScan"}}
<div>
<!-- stat is now an array containing key and value -->
{{stat.[0]}}: {{stat.[1]}}
</div>
{{/each}}
</template>