简体   繁体   中英

Mongo Insert Inside of a Meteor Method?

In my Meteor app, I have a Meteor method that takes the collection as a parameter and tries to run the mongo insert command on that collection to create a new document. The code runs every 10 seconds using setInterval .

Collection is defined:

My_Collection_Name = new Meteor.Collection('my_collection_name');


Server code:

 var collection = My_Collection_Name; var data = [1,2,3,'a','b','c']; Meteor.call('createDocument', collection, data); 


Method:

I20141030-14:58:06.716(-4)? Exception in setInterval callback: TypeError: Object #<Object> has no method 'insert'


However, this returns the following error in the console:

 I20141030-14:58:06.716(-4)? Exception in setInterval callback: TypeError: Object #<Object> has no method 'insert' 

Why doesn't this work? Is it possible to pass in the collection as a parameter? Thank you in advance!

What happens is that you have different collections on the client and on the server (read more at Understanding Meteor publish/subscribe ). Relying on the ~private _collection field is risky. What you do want is to indeed pass the collection name as a string to Meteor.call, make sure you have the same variable name on the server for the collection, and look up the collection by its name in the global object on the server:

// on the client:

MyCollection = new Mongo.Collection('record-set-name');  // set server-side by .publish()
Meteor.call('createDocument', 'MyCollection', data);

// on the server:
MyCollection = new Mongo.Collection('collection-name-in-mongo');
Meteor.methods({
  createDocument: function(collectionName, data) {
    global[collectionName].insert({
      data: data
    });
  }
});

This pattern is used by meteor-autocomplete to pass collection names from the client to the server .

Understanding Meteor publish/subscribe explains how the collection name and collection variable name relate in Mongo.

After further inspection of the collection object, I noticed that it has a _collection object inside which, in turn, contains the insert , update , etc. functions. Therefore, adjusting the code to the below solves the problem for running Mongo commands against a collection parameter inside a Meteor method:

Meteor.methods({
  createDocument: function(collection, data) {
    collection._collection.insert({
      data: data
    });
  }
});

I think here collection variable is taking as a collection name, If you have few collection names you can do it like below in the server methods

Meteor.methods({
  createDocument: function(collection, data) {
    if(collection == "firstcollection")
    {
      firstcollection.insert({
        data: data
      });
   }
   else if(collection == "secondcollection")
    {
      secondcollection.insert({
        data: data
      });
   }
  }
});

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