Meteor:阅读简单的JSON文件

Jas*_*mid 8 json meteor

我正在尝试使用Meteor读取JSON文件.我已经在stackoverflow上看到了各种答案,但似乎无法让它们工作.我试过这个 基本上说:

  1. 使用以下内容创建名为private/test.json的文件:
[{"id":1,"text":"foo"},{"id":2,"text":"bar"}]
Run Code Online (Sandbox Code Playgroud)
  1. 在服务器启动时读取文件内容(server/start.js):
Meteor.startup(function() {
 console.log(JSON.parse(Assets.getText('test.json')));
});
Run Code Online (Sandbox Code Playgroud)

然而,这个看似非常简单的示例不会将任何内容记录到控制台.如果我试着将它存储在一个变量而不是console.logging它然后显示它客户端我得到

Uncaught ReferenceError: myjson is not defined 
Run Code Online (Sandbox Code Playgroud)

myjson是我存储它的变量.我试过阅读JSON客户端

    Template.hello.events({
    'click input': function () {
        myjson = JSON.parse(Assets.getText("myfile.json"));
        console.log("myjson")
  });
}
Run Code Online (Sandbox Code Playgroud)

结果如下:

Uncaught ReferenceError: Assets is not defined 
Run Code Online (Sandbox Code Playgroud)
  1. 如果已经尝试了这里描述的所有选项:在Meteor中导入JSON文件的结果或多或少相同.

希望有人可以帮助我

小智 3

服务器方法就可以了,去掉多余的分号(;)就可以了。您在客户通话中需要更多信息。JSON 数据来自回调。

在您的点击事件中使用它:

if (typeof console !== 'undefined'){
    console.log("You're calling readit");
    Meteor.call('readit',function(err,response){
        console.log(response);
    });
}
Run Code Online (Sandbox Code Playgroud)

流星!