Meteor查询在服务器上运行良好,但在客户端上运行不好
因此,我有一个简单的服务器文件:Meteor查询在服务器上运行良好,但在客户端上运行不好,meteor,meteor-blaze,meteor-collections,Meteor,Meteor Blaze,Meteor Collections,因此,我有一个简单的服务器文件: import { Meteor } from 'meteor/meteor'; const Animals = new Mongo.Collection("animals"); let animalsFindOne = Targets.findOne({animal: "henry"}); console.log(_.get(animalsFindOne, 'food.favorite.amount')); 以及呈现
import { Meteor } from 'meteor/meteor';
const Animals = new Mongo.Collection("animals");
let animalsFindOne = Targets.findOne({animal: "henry"});
console.log(_.get(animalsFindOne, 'food.favorite.amount'));
以及呈现到模板的anives.js
文件
import {Template} from "meteor/templating";
import {Mongo} from "meteor/mongo";
import "/imports/ui/targets/animals.html";
const Animals = new Mongo.Collection("animals");
let animalsFindOne = Targets.findOne({animal: "henry"});
Template.targets.helpers({
foodAmount: function() {
return _.get(animalsFindOne, 'food.favorite.amount';
}
});
我可以将“foo”作为foodAmount返回,模板会将其呈现得非常好。对于.get
我使用erasaur:meteor-lodash
,它在server.js
中工作得非常好。在服务器控制台中,输出为“5”,这是预期的、非常好的输出。我在这里缺了什么?
编辑:我已经安装了autopublish,我不希望删除它,因为这个软件是一个测试。在
foodAmount
帮助程序之外已经定义了animalfindone
,因此它不会触发模板基于反应的重画机制
为了在助手中获得反应能力,您需要在助手中调用查询:
import{Template}来自“meteor/templating”;
从“meteor/Mongo”导入{Mongo};
导入“/imports/ui/targets/animals.html”;
const Animals=新蒙戈收藏(“动物”);
Template.targets.helpers({
foodAmount:function(){
让animalsFindOne=Targets.findOne({animal:henry});
return u.get(animalsFindOne,'food.favorite.amount';
}
});
编辑:Meteor允许使用最新版本,所以此处不需要lodash:
Template.targets.helpers({
foodAmount:function(){
让animalsFindOne=Targets.findOne({animal:henry});
返回动物完成?食物?最爱?数量
}
});
嗯,我想我需要阅读更多关于流星反应性的资料。但非常感谢!流星可选链接的建议也不错。我找不到该功能,然后改为使用lodash:D谢谢!