[SOLVED] Cannot Read JSON File using Assets.getText

Hi,

I am struggling to read a file with Assets.getText / getBinary and the ‘fs’ package.

I have tried this so far:

Assets.getText('/currencies.json', (err, res) => {
    if(err) console.log(err);

    console.log(res)
})

As well as the same code with getBinary. And also the usual:

const fs = require('fs');
const file = fs.readFileSync('path', 'utf8');

With ‘path’ being variations of the path to the file:

  • /imports/utils/users/currencies.json
  • ./currencies.json
  • /currencies.json

I have tried putting the file in a random folder inside /imports, inside a /private folder and inside a /public folder but I’m not sure what I’m missing?

Assets functions are all server side and read files from the private directory.

private/data.json

{
 "foo": "bar"
}

server/main.js

Meteor.startup(() => {
  Assets.getText('data.json', (err, data) => {
    if(err) console.log(err);else console.log(EJSON.parse(data));
  });
}
2 Likes

Thank you! That worked.