Meteor.method 函数中语法的含义是什么?
What is the meaning of the syntax in the Meteor.method function?
示例来自 https://www.meteor.com/tutorials/react/security-with-methods
Meteor.methods({
'tasks.insert'(text) {
check(text, String);
// Make sure the user is logged in before inserting a task
if (! this.userId) {
throw new Meteor.Error('not-authorized');
}
}
看起来它接受的唯一参数是 Object
类型,内置于 JS 中。但是,令人困惑的部分是它使用字符串来定义该对象中函数的名称。
为什么我不能
tasksInsert(text) {
// ...
}
在class或JS对象范围内使用字符串定义方法名是可以接受的。
j@j-desktop:~$ node
> function 'add'(x, y) {
function 'add'(x, y) {
^^^^^
SyntaxError: Unexpected string
> 'add'(x, y) {
... function 'add'(x, y) {
^^^^^
SyntaxError: Unexpected string
> 'add'(x, y) {
... return x + y;
... }
...
... ;
...
> class A {
... 'add'(x, y) {
..... return x + y;
..... }
...
... }
undefined
> let a = new A();
undefined
> a.add(1, 1);
2
> let obj = {
... 'addTwo'(x) {
..... return x + 2;
..... }
... }
undefined
> obj.addTwo(4);
6
Metor.methods()
定义了 Meteor 应用程序的 server-side 方法。
这不是一个简单的函数调用。这样您就可以定义客户端将调用哪些函数。
这是模板与数据库交互、检查、验证和进行更改的一种方式。
您只能调用:Meteor.call('stringName_ofFunction', {params}, (err, result)=>{CallBack});
如果您之前将此 'stringName_ofFunction'
定义为 Meteor.methods();
中的函数。
事实是,随着您的应用程序变得越来越大,您可以为服务器端方法定义不同的文件,这样您就可以变得更有条理。您将使用 Meteor.Methods();告诉 Meteor 该方法中的那些函数是 client-side.
上的 Meteor.call() 调用的服务器端函数
示例来自 https://www.meteor.com/tutorials/react/security-with-methods
Meteor.methods({
'tasks.insert'(text) {
check(text, String);
// Make sure the user is logged in before inserting a task
if (! this.userId) {
throw new Meteor.Error('not-authorized');
}
}
看起来它接受的唯一参数是 Object
类型,内置于 JS 中。但是,令人困惑的部分是它使用字符串来定义该对象中函数的名称。
为什么我不能
tasksInsert(text) {
// ...
}
在class或JS对象范围内使用字符串定义方法名是可以接受的。
j@j-desktop:~$ node
> function 'add'(x, y) {
function 'add'(x, y) {
^^^^^
SyntaxError: Unexpected string
> 'add'(x, y) {
... function 'add'(x, y) {
^^^^^
SyntaxError: Unexpected string
> 'add'(x, y) {
... return x + y;
... }
...
... ;
...
> class A {
... 'add'(x, y) {
..... return x + y;
..... }
...
... }
undefined
> let a = new A();
undefined
> a.add(1, 1);
2
> let obj = {
... 'addTwo'(x) {
..... return x + 2;
..... }
... }
undefined
> obj.addTwo(4);
6
Metor.methods()
定义了 Meteor 应用程序的 server-side 方法。
这不是一个简单的函数调用。这样您就可以定义客户端将调用哪些函数。
这是模板与数据库交互、检查、验证和进行更改的一种方式。
您只能调用:Meteor.call('stringName_ofFunction', {params}, (err, result)=>{CallBack});
如果您之前将此 'stringName_ofFunction'
定义为 Meteor.methods();
中的函数。
事实是,随着您的应用程序变得越来越大,您可以为服务器端方法定义不同的文件,这样您就可以变得更有条理。您将使用 Meteor.Methods();告诉 Meteor 该方法中的那些函数是 client-side.
上的 Meteor.call() 调用的服务器端函数