流星:读取简单的JSON文件 [英] Meteor: reading simple JSON file
问题描述
我正在尝试使用Meteor读取JSON文件.我已经在stackoverflow上看到了各种答案,但似乎无法使它们起作用.我已经尝试过此,它的基本含义是:
I am trying to read a JSON file with Meteor. I've seen various answers on stackoverflow but cannot seem to get them to work. I have tried this one which basically says:
- 创建一个名为private/test.json的文件,其内容如下:
[{"id":1,"text":"foo"},{"id":2,"text":"bar"}]
- 在服务器启动时读取文件内容(server/start.js):
Meteor.startup(function() {
console.log(JSON.parse(Assets.getText('test.json')));
});
但是,这个看似非常简单的示例并未将任何内容记录到控制台.如果我尝试将其存储在一个变量中,而不是在console.logging上,然后在客户端显示出来,我会得到
However this seemingly very simple example does not log anything to the console. If I trye to store it in a variable instead on console.logging it and then displaying it client side I get
Uncaught ReferenceError: myjson is not defined
myjson是我存储在其中的变量.我尝试读取JSON客户端
where myjson was the variable I stored it in. I have tried reading the JSON client side
Template.hello.events({
'click input': function () {
myjson = JSON.parse(Assets.getText("myfile.json"));
console.log("myjson")
});
}
产生的结果:
Uncaught ReferenceError: Assets is not defined
- 如果尝试了此处描述的所有选项:在Meteor中导入JSON文件具有大致相同的结果.
- If have tried all of the options described here: Importing a JSON file in Meteor with more or less the same outcome.
希望有人可以帮助我
推荐答案
服务器方法正常,只需删除多余的分号(;).客户呼叫中您还需要更多. JSON数据来自回调.
The server method is OK, just remove the extra semi-colon(;). You need a little more in the client call. The JSON data comes from the callback.
在点击事件中使用它:
if (typeof console !== 'undefined'){
console.log("You're calling readit");
Meteor.call('readit',function(err,response){
console.log(response);
});
}
流星!
这篇关于流星:读取简单的JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!