halapgos1
halapgos1

Reputation: 1195

Calling a method from client to server (Meteor)

Client Side

Users = new Mongo.Collection("user-info");
if (Meteor.isClient) {
    var myApp = angular.module('calorie-counter', ['angular-meteor']);
    myApp.controller('formCtrl', ['$scope', function ($scope) {

        $scope.user = {
            item1: 0,
            item2: 0
        };

        $scope.submit = function () {
            Meteor.call("submit" ($scope.user));
        }
    }]);
}

Server side:

if (Meteor.isServer) {
    Meteor.methods({
        submit: function (user) {
            Users.insert(user);
        }
    });
}

What I'm attempting to do is when the user clicks on the submit button on the client side, I want it to call a server side method where the information that the User entered will be saved into the collection. I'm passing in the $scope.user as a parameter (not too sure if I'm calling the method correctly) but the error I keep receiving is "submit is not a function". Initially, I was just inserting the $scope.user directly from that function, but I thought that type of an operation might be more suitable for the server side? (I'm not too sure if I'm thinking of this right or just overthinking)

Upvotes: 0

Views: 353

Answers (2)

Tushar
Tushar

Reputation: 87233

You need to use , after method name to pass parameters

Meteor.call("submit", $scope.user);
//           ^^^^^^^  ^^^^^^^^^^^
//           Method   Parameter

Documentation

Example

For multiple parameters, use comma-separator between parameters

// sync call
var result = Meteor.call('foo', 1, 2);

Also, all the database operations should be done on Server side for security. Otherwise end users(hackers) can gain the access to database and can attack on your DB.

Make sure you move all the database handling code on server and remove the package named insecure. This is the package that allow you to access DB from client-side.

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167240

This isn't a valid syntax:

Meteor.call("submit"($scope.user));

You need to pass it as a parameter. Please change it to:

Meteor.call("submit", $scope.user);

Upvotes: 0

Related Questions