0

I've got this code that goes on over and over again. It inserts a new document into a Meteor Mongo collection called Services, which is a global object that is instantiated already in another file (Services = new Mongo.Collection("services")).

  Services.insert({
    sku: 'hdrPhotos',
    price: 100
  });
  Services.insert({
    sku: 'twilightPhotos',
    price: 100
  });
  Services.insert({
    sku: 'videoClips',
    price: 175
  });

I want to write a function that takes the collection name and an array of objects to insert, but I'm not sure how to reference the collection name as a variable in my function:

var insertIntoCollection = function(collectionName, arrayOfObjects){

  for (index in arrayOfObjects){
    // doesn't work
    // collectionName.insert(arrayOfObjects[index]);
  };

};

It would be called like

  var serviceItems = [{
    sku: 'hdrPhotos',
    price: 100
  },{
    sku: 'twilightPhotos',
    price: 100
  },{
    sku: 'videoClips',
    price: 175
  }];

  insertIntoCollection("Services", serviceItems);
fuzzybabybunny
  • 5,146
  • 6
  • 32
  • 58

1 Answers1

1

Try

insertIntoCollection(Services, serviceItems);

.... With your previous code of "Services" being a string, you were essentially calling this in the function:

"Services".insert(arrayOfObjects[index]);

Which is obviously not the end result you want...


On the side note, it's discouraged to use a key in object loop on arrays... Try looping through it like this instead:

var insertIntoCollection = function(collection, dataArray){
  for (var index=0; index<dataArray.length; index++){
    collection.insert(dataArray[index]);
  };
};

Or as an alternative, you could use the .forEach method implemented in ECMAScript 5.1... In you're case, you'd use it like this:

var insertIntoCollection = function(collection, dataArray){
  dataArray.forEach(function(item){
    collection.insert(item);
  });
};
  • I feel really really really dumb right now. Not because of the string thing (I knew that it wouldn't work), but because I didn't think to just chuck in the `Services` object directly as an argument. I thought there was something wrong with the function's code but I just wasn't calling it correctly. – fuzzybabybunny Jun 13 '15 at 05:15
  • Hmmm... why is it discouraged to use `key in object`? In my case it'll just run the contents of the for-in loop on whatever object is contained in the index of the array, which are all uniform. – fuzzybabybunny Jun 13 '15 at 05:16
  • 1
    [See this question and answer](http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript) for a in-depth explanation on why it's discouraged. –  Jun 13 '15 at 05:20
  • Oh wow that is weird. So for-in is only safe for objects? I use for-in because it's so quick to type compared to the normal for loop. But I guess with arrays there isn't a shorter way to safely loop through them? – fuzzybabybunny Jun 13 '15 at 05:24
  • 1
    @fuzzybabybunny One fast way is to use the [`.forEach`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) method on arrays. This was set as a standard in ECMAScript 5.1. (Editing answer to include the `.forEach` as an alternative to the current loop) –  Jun 13 '15 at 05:28
  • Wow, thanks for bringing this up. Learn something new everyday. This potentially has saved me hours of debugging. Are there any good resources that list all of the "javascript quirks and best practices"? I know that there are lots of other things like equality operators sometimes not working as expected, copying objects (deep vs. shallow), etc. – fuzzybabybunny Jun 13 '15 at 05:34
  • You can find answers to a lot of those here in-house (StackOverflow). But places like [MDN (Mozilla Developer Network)](https://developer.mozilla.org/en-US/docs/Web/JavaScript) and [W3C Standards](http://www.w3.org/standards/) contain a lot of useful and trusted information too. (You can add me on Skype at xerodev if you want to chat further :3) –  Jun 13 '15 at 05:37