简体   繁体   中英

How do I trigger events using Meteor server side events

I am using Meteor, which uses Mongodb as its database. I have code that inserts several documents into a collection when users fill out a form. When these documents are inserted, I would like to fire some JavaScript code within the server side directories that sorts through the collection in question for documents with matching fields as the documents just inserted.

My problem is that I do not know how to fire code on the server when the new documents arrive. Would it make sense to Meteor.call a Meteor.method at the end of the code involved with inserting, with the Meteor.method called preforming the sorting code I need?

Edit:

As you can see, in the below code I'm not calling any Meteor methods as none exist yet. The vast majority of this code is simply lead up for the insert({}) at the end of the page, so I think it can be safely ignored. The only server side code I have is to declare the possibleGames mongo collection.

I am not sure what you mean by call a plain JavaScript function, my problem is getting any code firing at all.

possibleGames = new Mongo.Collection("possibleGames");

Template.meet_form.events({
"submit .meet_form": function(event, template){
event.preventDefault();

var user = Meteor.userId();
var where = event.target.where.value;

var checkedGames = [];
function gameCheck (game) {
  if (game.checked === true){
    checkedGames.push(game.value);
  };
};

var checkedDays = [];
function dayCheck (day) {
  if (day.checked === true){
    checkedDays.push(day.value);
  };
};

console.log(event.target.where.value)

gameCheck(event.target.dnd);
gameCheck(event.target.savageWorlds);
gameCheck(event.target.shadowRun);
console.log(checkedGames);

dayCheck(event.target.monday);
dayCheck(event.target.tuesday);
dayCheck(event.target.wednesday);
dayCheck(event.target.thursday);
dayCheck(event.target.friday);
dayCheck(event.target.saturday);
dayCheck(event.target.sunday);
console.log(checkedDays);

var whereWhat = [];
for (i = 0; i < checkedGames.length; i++) {
  var prepareWhereWhat = where.concat(checkedGames[i]);
  whereWhat.push(prepareWhereWhat);
};
console.log(whereWhat);

var whereWhatWhen = [];
for (a = 0; a < whereWhat.length; a++) {
  var prepareWWW1 = whereWhat[a];
  for (b = 0; b < checkedDays.length; b++) {
    var prepareWWW2 = prepareWWW1.concat(checkedDays[b]);
    whereWhatWhen.push(prepareWWW2);
  };
};
console.log(whereWhatWhen);


for (i = 0; i < whereWhatWhen.length; i++) {
  possibleGames.insert({
    game: whereWhatWhen[i],
    user: user,
    created_on: new Date().getTime()
    })
}

}
});

You don't need to do a meteor.call on the server because you're already on the server. Just call a plain javascript function.

If what you want to call from your first Meteor.method is already in another Meteor.method, then refactor that function to extract out the common bit.

Some code would also help if this is still confusing.

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