0

So I'm trying to create a global variable that copies the MongoDB database into a new array, so I keep server calls at a minimum.

It looks like this:

db = _.map(MyCollection.find().fetch()[0], function(item){return item});

I place this into my /lib/constants.js folder to make sure it loads before anything else in client. I place it inside client, it works.

Ultimately, the db as a global variable doesn't work. Not sure what I'm doing wrong. Also, if you would be kind to answer this question also: Can I do regular JS statements like loops inside the lib folder to make the result load before the client does?

Thanks!

Mabeh Al-Zuq Yadeek
  • 46
  • 4
  • 16
  • 28
  • By it doesn't work, it's not reachable by helper functions on client when I need to call it. It's basically like it doesn't exist but no exception errors happen. When I transplant it into client to a helper function, it works fine and all the data that I need to get out of the new db variable show up. – Mabeh Al-Zuq Yadeek Oct 20 '15 at 19:10
  • Can you take a look at [this question](http://stackoverflow.com/q/26836390/4174897) and see if it answers yours? – Kyll Oct 20 '15 at 19:11
  • I saw that before and nothing I didn't know. Let me update this: It works fine if I manipulate the data inside the /lib/constants.js file. However, if I want to take the db variable and try to manipulate it outside the /lib/constants.js file, everything crashes and the original script won't function. I guess I have to write all the javascript in constants js. Which is ok with me. – Mabeh Al-Zuq Yadeek Oct 20 '15 at 19:20
  • It sounds like some load order issue which is why I pointed you to this question. Are you sure the `db` variable is initialized before you are trying to access it? – Kyll Oct 20 '15 at 19:24
  • I don't know. That's what I'm actually trying to do by placing it in the lib folder, to load it before anything else in client. Sorry for being so noobish about this. – Mabeh Al-Zuq Yadeek Oct 20 '15 at 19:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92894/discussion-between-kyll-and-mabeh-al-zuq-yadeek), it's getting quite lenghty. – Kyll Oct 20 '15 at 19:35
  • 1
    A global subscription for the collection would give the same effect on the client side, and give you the ability to query the data easily. – Brian Shamblen Oct 21 '15 at 03:08
  • I removed autopublish, subscribed to the collection for the specific route via iron router and still have issues with the variables (located in lib folder) not being able to be called on client. Load order is normal. lib/constants.js file loads first, but for some reason I can't access the javascript and variables I have assigned in the lib/constants.js from the client side. Ugh, I don't even know what to do anymore. – Mabeh Al-Zuq Yadeek Oct 21 '15 at 16:06

0 Answers0