简体   繁体   English

从客户端到服务器(流星)调用方法

[英]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". 我将$scope.user作为参数传递(不太确定我是否正确调用了该方法),但我一直收到的错误是“提交不是函数”。 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? 最初,我只是直接从该函数中插入$scope.user ,但是我认为一种操作类型可能更适合服务器端? (I'm not too sure if I'm thinking of this right or just overthinking) (我不太确定我是在考虑这个权利还是只是在想太多)

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 . 确保在服务器上移动所有数据库处理代码,并删除名为insecure的软件包。 This is the package that allow you to access DB from client-side. 这是允许您从客户端访问数据库的软件包。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM