Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor pass data from client to server

I have a registration form, and when the user clicks the submit button the value in every textbox will be sent to server to insert that data, and return true/false.

Client:

Template.cust_register.events({
    'click button': function(){
          var email = $('#tbxCustEmail').val();
          var msg = $('#tbxCustMsg').val();
          var isSuccess = insertMsg(email,msg);
          if(isSuccess){
             alert("Success");
          }else alert("Try again");
    }
});

Server:

function insertMsg(email,msg){
     Messages.insert({Email:email,Message:msg});
     return true;
}

This turned out to not work. How to solve this? Many people said "use publish/subscribe", but I don't understand how to use that.

like image 991
yozawiratama Avatar asked Feb 14 '23 08:02

yozawiratama


1 Answers

First, watch the introductory screencast and read the Data and security section of the docs.

Your code in a publish/subscribe model would look like this:

Common:

Messages = new Meteor.Collection('messages');

Client:

Meteor.subscribe("messages");

Template.cust_register.events({
    'click button': function(){
          var email = $('#tbxCustEmail').val();
          var msg = $('#tbxCustMsg').val();
          Messages.insert({Email:email,Message:msg});
    }
});

Server:

Meteor.publish("messages", function() {
    return Messages.find();
});
like image 116
rzymek Avatar answered Feb 17 '23 02:02

rzymek