-1

I'm trying to figure out how to add a new collection each time a button is pressed. I have this html:

html:

<template name="tempName">
    <button class="submitButton">Submit</button>
</template>

javascript:

Template.tempName.events({
    'click .submitButton': function() {
      count += 1;
      Npm.newCol = new Mongo.Collection("NUM:" + count);
      Npm.newCol.insert({
        field1: "field1 contents",
        field2: "field2 contents"
      });
    }
  });

This does absolutely nothing, as far as I can tell. If I put all the contents from the .submitButton click event at the top of the js file (outside of the "if (Meteor.isClient)"), then it works perfectly. But I would like a new collection to be created each time a form is submitted. Does anyone know how to accomplish this?

user3345626
  • 223
  • 1
  • 3
  • 13
  • Are you looking to insert a new document into an existing collection? Dynamically created collections are almost never the correct solution to any problem in meteor. – David Weldon Jul 22 '15 at 22:03
  • Unfortunately, each form submission will automatically generate as many as hundreds of new documents within a collection, and I need them to be separate-- I can't have all the documents from various runs all in the same collection. So perhaps it's bad, but I'm pretty sure dynamically created collections is the correct solution in my case. – user3345626 Jul 22 '15 at 22:18
  • 1
    Well, you can create unmanaged client collections pretty easily, but creating a new collection that the server knows about is actually [really tricky](https://stackoverflow.com/questions/15214667/creating-new-meteor-collections-on-the-fly). – David Weldon Jul 22 '15 at 22:35
  • Oh dang. I hadn't seen that, thanks. – user3345626 Jul 22 '15 at 22:40

1 Answers1

0

It's easy enough, just call a method that has access to the server so it creates the local collection and the Mongo collection.

That said, if you're allowing users to create collections, I'm 99% sure you're gonna have a bad time...

Matt K
  • 4,813
  • 4
  • 22
  • 35