How to Use User JSON file in Meteor?

后端 未结 1 698
被撕碎了的回忆
被撕碎了的回忆 2021-01-02 18:30

I need to know the JSON file usage in Meteor JS. First where the JSON file store in Meteor Folder Structure and How to get JSON Data using Meteor JS and is it JSON file save

1条回答
  •  时光说笑
    2021-01-02 18:53

    If you want to read JSON data, you can use the assets api. You can do the following test in any project:

    1) Create a file called private/test.json with the following contents:

    [{"id":1,"text":"foo"},{"id":2,"text":"bar"}]
    

    2) Read the file contents when the server starts (server/start.js):

    Meteor.startup(function() {
      console.log(JSON.parse(Assets.getText('test.json')));
    });
    

    Here we are using getText to read the contents of the file (it assumes the file is located in the private directory). Then we are passing the JSON string contents to parse which will return an object.

    Note that the file extension (.json) does not matter, however it is conventional to use it.

    0 讨论(0)
提交回复
热议问题