在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ Meteor方法
Meteor結(jié)構(gòu)
Meteor部署
Meteor排序
Meteor事件
Meteor Blaze
Meteor第一個(gè)應(yīng)用程序
Meteor發(fā)布和訂閱
Meteor環(huán)境安裝配置
Meteor package.js
Meteor在手機(jī)上運(yùn)行
Meteor集合
Meteor模板
Meteor跟蹤器
Meteor發(fā)送郵件
Meteor計(jì)時(shí)器
Meteor ToDo App實(shí)例
Meteor軟件包管理
Meteor方法
Meteor表單
Meteor Assets資源
Meteor會(huì)話
Meteor EJSON
Meteor http
Meteor安全
Meteor核心API
Meteor check
Meteor帳號(hào)
Meteor教程

Meteor方法

Meteor方法是寫入在服務(wù)器側(cè)的函數(shù),但可以在客戶端調(diào)用這些函數(shù)。

在服務(wù)器端,我們將創(chuàng)建兩個(gè)簡(jiǎn)單的方法。第一個(gè)參數(shù)將加5,而第二個(gè)參數(shù)將加10。

使用方法

meteorApp/server/main.js

if(Meteor.isServer) {
   Meteor.methods({
	
      method1: function (arg) {
         var result = arg + 5;
         return result;
      },

      method2: function (arg) {
         var result = arg + 10;
         return result;
      }
   });
}

meteorApp/client/app.js

	
if(Meteor.isClient) {
   var aaa = 'aaa'
   Meteor.call('method1', aaa, function (error, result) {
	
      if (error) {
         console.log(error);
         else {
            console.log('Method 1 result is: ' + result);
         }
      }
   );

   Meteor.call('method2', 5, function (error, result) {
      if (error) {
         console.log(error);
      } else {
         console.log('Method 2 result is: ' + result);
      }
   });
}
當(dāng)我們啟動(dòng)應(yīng)用程序,我們將看到在控制臺(tái)輸出的計(jì)算值。


處理錯(cuò)誤

如需處理錯(cuò)誤,可以使用 Meteor.Error 方法。下面的例子說明如何為未登錄用戶處理錯(cuò)誤。

meteorApp/server/main.js
import { Meteor } from 'meteor/meteor';
Meteor.startup(() => {
  // code to run on server at startup
});
if(Meteor.isServer) {
   Meteor.methods({
      method1: function (param) {
         if (! this.userId) {
            throw new Meteor.Error("logged-out",
               "The user must be logged in to post a comment.");
         }
         return result;
      }
   });
}

meteorApp/client/app.js

if(Meteor.isClient) {  Meteor.call('method1', 1, function (error, result) {
   if (error && error.error === "logged-out") {
      console.log("errorMessage:", "Please log in to post a comment.");
   } else {
      console.log('Method 1 result is: ' + result);
   }});
}
控制臺(tái)將顯示我們的自定義錯(cuò)誤消息。



上一篇:Meteor集合下一篇:Meteor核心API