我想在客户端显示一些数据点,我想从存储在服务器上的文件中获取纬度,经度和摘要。
我已经阅读了许多帖子,说使用Meteor方法使用papaParse,但我无法使其工作。
你们能指出我正确的方向,我的问题是:
.txt
,.csv
或.json
文件?答案 0 :(得分:1)
您可以将静态文件放入服务器上的private
文件夹中,然后通过Assets
。{/ p>
例如,您的data.json
文件夹中有private
个文件。
获取此数据的方法:
Meteor.methods({
getData() {
return JSON.parse(Assets.getText('data.json'));
}
});
您现在可以在客户端上调用此方法:
Meteor.call('getData', function(err, res) {
console.log(res);
});
<强> UPD 强>
好的,如何显示它。
Meteor.call
运行异步,因此我们将使用反应来更新我们对结果的看法。
以下是我们如何在ourData
模板上显示数据。
<template name="ourData">
<!-- Here you may want to use #each or whatever -->
<p>{{ourData}}</p>
</template>
Template.ourData.onCreated(function() {
this.ourData = new ReactiveVar();
Meteor.call('getData', (err, res) => {
if (err) {
console.error(err);
} else {
// Putting data in reactive var
this.ourData.set(res);
}
});
});
Template.ourData.helpers({
ourData: function() {
// Helper will automatically rerun on method res
return Template.instance().ourData.get();
}
});
需要 reactive-var
个包,或者您也可以使用Session
。