JSON file at the root folder of the application crashes meteor

Guys I’m trying to load a JSON both in the server and the client given the following folder structure.

.
├── README.md
├── client
│   ├── home
│   │   ├── controllers
│   │   │   ├── home.controller.ng.js
│   │   │   └── products.controller.ng.js
│   │   └── views
│   │       ├── home.ng.html
│   │       └── products.ng.html
│   ├── lib
│   │   └── app.ng.js
│   ├── routes.ng.js
│   └── styles
│       └── main.styl
├── index.html
├── model
│   └── products.js
├── products.json
└── server

Here is a gist where I’m collecting critical information related to the issue.

I’m aware of this SO answer but that doesn’t give me any clue about how I’m doing things wrong.

So if anybody can figure out what’s going on or have a clear explanation about why I can’t do it like that, please reach me as soon as you can.

Your assistance is very much appreciated.

In order to be able to load an asset with HTTP GET it needs to be in the /public directory of your application

1 Like

By that you mean both styles will work the same way if I put the json inside the public folder? I thought server side code could not reach the public folder. Anyway I tried it and it didn’t worked, unbelievable.

Commenting lines 8 to EOF from model/products.js makes the server at least start up to log the json array into the browser console.

// model/products.js

if (Meteor.isClient) {
  // Should execute only on the Client Side.
  HTTP.get(Meteor.absoluteUrl("products.json"), function(err,result) {
      if (result) { console.log(result.data); }
  });
}

// if (Meteor.isServer) {
//   // Should execute only on the Server Side.
//   Products = HTTP.get(Meteor.absoluteUrl("products.json")).data;
//   console.log(Products);
// }

So the question is. How do I load json data from a json file that lives in the root folder of my meteor project and have it available through a variable at the same time in the client and the server pretty much the same way we can do with a mongo collection.

You can’t load a file from the root folder. I would put the json file into the private folder and load it with getAsset, put it into a collection and the client will get the data through pub/sub of the collection, or by calling a method

Makes sense @jamgold. I just can’t understand why meteor doesn’t throw me a warning and move forward to startup the server instead of just staying hung waiting for a response from a route it already know is impossible to reach.

Consider the following situation. I have now my json file inside the public directory and I’m trying to retrieve it with the following code inside my model/products.js

// if (Meteor.isClient) {
//   // Should execute only on the Client Side.
//   HTTP.get(Meteor.absoluteUrl("products.json"), function(err,result) {
//       if (result) { console.log(result.data); }
//   });
// }

if (Meteor.isServer) {
  // Should execute only on the Server Side.
  Products = HTTP.get(Meteor.absoluteUrl("products.json")).data;
  console.log(Products);
}

Within this scenario, as when I have no commented line inside my model/products.json, the server never fires up and the following error pops out after a arbitrary amount of time.

$ meteor
[[[[[ ~/Projects/Tevincocin/frescaentrega-angular-meteor ]]]]]

=> Started proxy.                             
=> Started MongoDB.                           
W20150804-16:04:34.176(-3)? (STDERR)          
W20150804-16:04:34.180(-3)? (STDERR) /Users/n370/.meteor/packages/meteor-tool/.1.1.4.rpwbv7++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:278
W20150804-16:04:34.180(-3)? (STDERR) 						throw(ex);
W20150804-16:04:34.180(-3)? (STDERR) 						      ^
W20150804-16:04:34.181(-3)? (STDERR) Error: socket hang up
W20150804-16:04:34.181(-3)? (STDERR)     at Object.Future.wait (/Users/n370/.meteor/packages/meteor-tool/.1.1.4.rpwbv7++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:398:15)
W20150804-16:04:34.181(-3)? (STDERR)     at Object.call (packages/meteor/helpers.js:119:1)
W20150804-16:04:34.181(-3)? (STDERR)     at Object.HTTP.get (packages/http/httpcall_common.js:45:1)
W20150804-16:04:34.181(-3)? (STDERR)     at app/model/products.js:10:19
W20150804-16:04:34.181(-3)? (STDERR)     at app/model/products.js:14:3
W20150804-16:04:34.182(-3)? (STDERR)     at /Users/n370/Projects/Tevincocin/frescaentrega-angular-meteor/.meteor/local/build/programs/server/boot.js:222:10
W20150804-16:04:34.182(-3)? (STDERR)     at Array.forEach (native)
W20150804-16:04:34.182(-3)? (STDERR)     at Function._.each._.forEach (/Users/n370/.meteor/packages/meteor-tool/.1.1.4.rpwbv7++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20150804-16:04:34.182(-3)? (STDERR)     at /Users/n370/Projects/Tevincocin/frescaentrega-angular-meteor/.meteor/local/build/programs/server/boot.js:117:5
W20150804-16:04:34.182(-3)? (STDERR)     - - - - -
W20150804-16:04:34.182(-3)? (STDERR)     at createHangUpError (http.js:1477:15)
W20150804-16:04:34.182(-3)? (STDERR)     at Socket.socketOnEnd (http.js:1573:23)
W20150804-16:04:34.183(-3)? (STDERR)     at Socket.g (events.js:180:16)
W20150804-16:04:34.183(-3)? (STDERR)     at Socket.emit (events.js:117:20)
W20150804-16:04:34.183(-3)? (STDERR)     at _stream_readable.js:944:16
W20150804-16:04:34.183(-3)? (STDERR)     at process._tickCallback (node.js:442:13)
=> Exited with code: 8
W20150804-16:06:39.783(-3)? (STDERR)          
W20150804-16:06:39.784(-3)? (STDERR) /Users/n370/.meteor/packages/meteor-tool/.1.1.4.rpwbv7++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:278
W20150804-16:06:39.785(-3)? (STDERR) 						throw(ex);
W20150804-16:06:39.785(-3)? (STDERR) 						      ^
W20150804-16:06:39.805(-3)? (STDERR) Error: socket hang up
W20150804-16:06:39.805(-3)? (STDERR)     at Object.Future.wait (/Users/n370/.meteor/packages/meteor-tool/.1.1.4.rpwbv7++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:398:15)
W20150804-16:06:39.805(-3)? (STDERR)     at Object.call (packages/meteor/helpers.js:119:1)
W20150804-16:06:39.805(-3)? (STDERR)     at Object.HTTP.get (packages/http/httpcall_common.js:45:1)
W20150804-16:06:39.805(-3)? (STDERR)     at app/model/products.js:10:19
W20150804-16:06:39.805(-3)? (STDERR)     at app/model/products.js:14:3
W20150804-16:06:39.805(-3)? (STDERR)     at /Users/n370/Projects/Tevincocin/frescaentrega-angular-meteor/.meteor/local/build/programs/server/boot.js:222:10
W20150804-16:06:39.806(-3)? (STDERR)     at Array.forEach (native)
W20150804-16:06:39.806(-3)? (STDERR)     at Function._.each._.forEach (/Users/n370/.meteor/packages/meteor-tool/.1.1.4.rpwbv7++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20150804-16:06:39.806(-3)? (STDERR)     at /Users/n370/Projects/Tevincocin/frescaentrega-angular-meteor/.meteor/local/build/programs/server/boot.js:117:5
W20150804-16:06:39.806(-3)? (STDERR)     - - - - -
W20150804-16:06:39.806(-3)? (STDERR)     at createHangUpError (http.js:1477:15)
W20150804-16:06:39.806(-3)? (STDERR)     at Socket.socketOnEnd (http.js:1573:23)
W20150804-16:06:39.806(-3)? (STDERR)     at Socket.g (events.js:180:16)
W20150804-16:06:39.806(-3)? (STDERR)     at Socket.emit (events.js:117:20)
W20150804-16:06:39.806(-3)? (STDERR)     at _stream_readable.js:944:16
W20150804-16:06:39.806(-3)? (STDERR)     at process._tickCallback (node.js:442:13)
=> Exited with code: 8

Ask yourself “who is actually responding to the GET request”?

You are requesting a file in the server code that is responsible to answer that request, thus blocking itself.

Makes sense @jamgold. Thanks for taking your time to make things clear for me. I’m moving forward now using the assets api with my json file inside the private folder. I hope in the near future meteor updates to display a friendlier error message upon this kind of situation.