如何从 meteor.call 访问对象
how to access an object from meteor.call
只是想在 Meteor JS 中解决问题。我一直在尝试将 Meteor.call 结果对象传回模板。这是我的代码:
HTML 模板
<template name="showTotals">
<div class="container">
{{#each recordCnt}}
{{/each}}
</div>
</template>
客户端 JS
//get a count per item
Template.showTotals.recordCnt = function(){
Meteor.call('getCounts', function(err, result) {
if (result) {
//console.log(result); <-this shows object in javascript console
return result;
}
else {
console.log("blaaaa");
}
});
}
服务器 JS
Meteor.startup(function () {
Meteor.methods({
getCounts: function() {
var pipeline = [{$group: {_id: null, count: {$sum : 1}}}];
count_results = MyCollection.aggregate(pipeline);
return count_results;
}
});
});
全局 JS
MyCollection = new Meteor.Collection('folks');
我已尝试为调用结果添加 console.log(result),它显示在 javascript 控制台中并显示预期结果。但是我无法用它来填充 recordCnt。
有什么想法吗?
您无法按您希望的方式执行此操作,因为来自 Meteor.call
的回调是异步运行的。
但是,您可以使用回调来设置会话值并让您的助手读取 Session.get()
。
Template.showTotals.rcdCount = function() {
return Session.get('rcdCount');
}
Template.showTotals.rendered = function() {
Meteor.call('getCounts', function(err, result) {
if (result) {
Session.set('rcdCount', result);
}
else {
console.log("blaaaa");
}
});
}
请参阅 here 以获取包含 "hacky" 方法示例的类似答案。
只是想在 Meteor JS 中解决问题。我一直在尝试将 Meteor.call 结果对象传回模板。这是我的代码:
HTML 模板
<template name="showTotals">
<div class="container">
{{#each recordCnt}}
{{/each}}
</div>
</template>
客户端 JS
//get a count per item
Template.showTotals.recordCnt = function(){
Meteor.call('getCounts', function(err, result) {
if (result) {
//console.log(result); <-this shows object in javascript console
return result;
}
else {
console.log("blaaaa");
}
});
}
服务器 JS
Meteor.startup(function () {
Meteor.methods({
getCounts: function() {
var pipeline = [{$group: {_id: null, count: {$sum : 1}}}];
count_results = MyCollection.aggregate(pipeline);
return count_results;
}
});
});
全局 JS
MyCollection = new Meteor.Collection('folks');
我已尝试为调用结果添加 console.log(result),它显示在 javascript 控制台中并显示预期结果。但是我无法用它来填充 recordCnt。
有什么想法吗?
您无法按您希望的方式执行此操作,因为来自 Meteor.call
的回调是异步运行的。
但是,您可以使用回调来设置会话值并让您的助手读取 Session.get()
。
Template.showTotals.rcdCount = function() {
return Session.get('rcdCount');
}
Template.showTotals.rendered = function() {
Meteor.call('getCounts', function(err, result) {
if (result) {
Session.set('rcdCount', result);
}
else {
console.log("blaaaa");
}
});
}
请参阅 here 以获取包含 "hacky" 方法示例的类似答案。