简体   繁体   中英

pre-processing data into a hard-coded array

I have a question about hard-coded arrays. I looked at several previously posed questions about hard-coded arrays in hopes of getting my answer that way. But, for the most part, I don't understand the answers, and this is the only one that seems like it might be relevant:

glob() to build array of files, or hardcode array? Speed is key, but automation is nice

My question is a lot simpler, though. I have several worksheets in an OpenOffice spreadsheet which I have chosen to pre-process into a large hard-coded array which I will then store inside my 'server' dir. In order to test this, I put the following lines of code into a file called 'distances.js' and placed that file in a 'server' folder directly inside my app directory:

  var distances = {};
  distances['Salt Lake City.Washington, DC'] = 2080;
  distances['Salt Lake City.Cheyenne'] = 434;
  distances['Salt Lake City.Denver'] = 536;
  distances['Salt Lake City.Carson City'] = 534;

Then I ran the following command in my console to see if I'd be able to access these array values in my app:

  console.log(distances['Salt Lake City.Carson City']);

The result I got was:

Uncaught ReferenceError: scores is not defined(…)


I then attemped to insert those lines inside the regular project.js file inside the Meteor.startup function inside of Meteor.isServer:

  if (Meteor.isServer) {
    Meteor.startup(function () {
      // code to run on server at startup
      var distances = {};
      distances['Salt Lake City.Washington, DC'] = 2080;
      distances['Salt Lake City.Cheyenne'] = 434;
      distances['Salt Lake City.Denver'] = 536;
      distances['Salt Lake City.Carson City'] = 534;
    });
  }

This resulted in the same error.


I have the 'insecure' package installed in my project, so security shouldn't be an issue. I think I'm just missing something fundamental about where code needs to go in order to be seen by the compiler/interpreter. Can anyone help?

I'm sort of half expecting someone to suggest that I put all of this information into a collection. I don't currently understand why it would be advantageous to do so, but maybe I'm missing something fundamental about the usefulness of doing it this way. If so, could someone explain or point me to a place where I can read about this for myself? I have worked through a couple of meteor tutorials, most recently Your Second Meteor Application. And these are excellent tutorials from which I've learned a lot. But I feel like there are still holes in my knowledge which need to be addressed, this being a prime example.


My plan is to access these hard-coded array elements through a function call which looks something like this:

  getDistance('Salt Lake City','Cheyenne')

Because I don't store backwards values, eg. the distance from Cheyenne to Salt Lake City, I intend to set up the function so that, if a specific reference is undefined, it will turn the two elements around and call the function again the same way but with those inverted values (ie. getDistance('right','left') in place of getDistance('left','right')).

But, currently, I can't even get past step one.

Thanks in advance for any assistance you can provide me with.

The answer would seem to be to use the fs module to read data out of your textfile and into your collection. I'm still working on getting that going, but there's more info here: Using nodejs fs module within my meteor app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM