2017-04-22 6 views
0

この流星サーバー・コードはパブリックメソッドでMeteor.userId()を使用しようとすると「のsendEmail」が、時々私はMeteor.userIdしか呼び出すことができ、エラーMeteor.userIdの正しい使用()

エラーを取得しますメソッド呼び出しで。使用this.userId

それは固定することができますどのように
lib = (function() { 

    return Object.freeze({ 
    'sendEmail': function(msg){ 
     let userId = Meteor.userId(); 
     //do stuff for this user 
    }, 
    'otherPublicMethod': function(){ 
     //do other things then use sendEmail 
     lib.sendEmail(); // <---- Error Meteor.userId can only be invoked in method calls. Use this.userId 
    } 
    }); 
}()); 

// Now I call sendEmail from any where, or can I? 

Meteor.methods({ 
    'sendEmail': (msg) => { 
    lib.sendEmail(msg); // <---- NO error when this is called 
    }, 
}); 

? thx

+0

'userId()'は 'Publications '内では使用できませんが、' Methods'内で使用することはできません。 –

答えて

0

をシミュレートし、それ以下のコードを確認してください。代わりに、ES16 modulesを利用して共通の機能を定義することができます。

あなたが指定したとおり、Meteor.userId()はメソッド呼び出しで使用できますが、サーバー上のスタンドアロン関数では使用できません。メソッド呼び出しからこのような関数を呼び出すときに使用するパターンは、userId(または実際のユーザー)を渡すことです。例えば

輸入/ API /メール/サーバ/ utilsの/ emailUtils.js:

const SendEmail = function(userId, msg) { 
    // do stuff 
}; 

export {SendEmail}; 

輸入/ API /メール/サーバ/ emailMethods.js:今

import {SendEmail} from '/imports/api/email/server/utils/emailUtils'; 

Meteor.methods({ 
    'sendEmail': (msg) => { 
     check(msg, String); 
     // other security checks, like user authorization for sending email 
     SendEmail(Meteor.userId(), msg); 
    }, 
}); 

、あなたは再を持っています任意のメソッドから呼び出すことができる、または公開するSendEmail関数を使用できます。さらに、このパターンに従うことで、テスト可能なコードの作成に一歩近づくことができます。つまり、 "this.userId"または "Meteor.userId()"を擬似するよりも、userIdを注入している関数をテストする方が簡単です。

+0

IIFEのモジュールが好きな理由の1つは、funcName.methNameのようなパブリックメソッドを呼び出すことができ、呼び出すコードを知っていること、つまり書き込むコメントが少ないことです。 –

0

lib.sendEmailが非同期メソッドから呼び出されている場合は、Meteor環境 をバインドするようにしてください。私はそっとあなたがIIFEの使用を置き換える提案するつもりだ非同期動作

lib = (function() { 
    return Object.freeze({ 
     'sendEmail': function (msg) { 
      let userId = Meteor.userId(); 
      console.log(userId); 
      //do stuff for this user 
     }, 
     'otherPublicMethod': function() { 
      //do other things then use sendEmail 
      lib.sendEmail(); // <---- Error Meteor.userId can only be invoked in method calls. Use this.userId 
     } 
    }); 
}()); 

// Now I call sendEmail from any where, or can I? 

Meteor.methods({ 
    'sendEmail': (msg) => { 
     //simulate async behaviour + bind environment 
     Meteor.setTimeout(Meteor.bindEnvironment(function() { 
      lib.sendEmail(msg); // <---- NO error when this is called 
     })); 
     //output : 
     // null - if user has not logged in else 
     // actual userId - if user is loggedin 

     //simulate async behaviour without binding environment 
     Meteor.setTimeout(function() { 
      lib.sendEmail(msg); // <---- error when this is called 
     }); 
     //output : 
     // Exception in setTimeout callback: Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions. 
    }, 
}); 
関連する問題