私はReact with Meteorを使用しています。流星:非同期メソッドで作業する
// auth.js
Meteor.methods({
async 'auth.loginWithGoogle'(){
console.log('2')
let err = await Meteor.loginWithGoogle()
console.log('3');
console.log(err)
if (err) {
console.log('-1')
throw new Error(err);
}
console.log('4');
// Meteor.loginWithGoogle({
// // options
// }, (err) => {
// console.log('3')
// if (err) {
// console.log(err)
// throw new Meteor.Error(err)
// } else {
// console.log('4')
// // successful login!
// }
// });
}
});
// index.js
loginWithGoogle() {
console.log('1')
Meteor.call('auth.loginWithGoogle', {}, (err, res)=>{
console.log('5');
if (err) {
console.log(err) // line 16:
} else {
console.log('success');
// console.log(res);
// console.log('logged in with google callback in react yay:)')
}
});
}
:私は、この方法は、内部(同様にクライアント上で実行される)Meteor.methodを呼び出す成分を反応させました 注:Meteor.loginWithGoogleはaccounts-googleパッケージで提供されています。 テスト時にGoogleのログインページにアクセスしてサインインして、自分のアプリにリダイレクトしてログを出力できました。
ここでコメントされたコードは古いアプローチです。注意しておきたいのは、console.logの番号付きの呼び出しです。番号は、コードの実行順序を示しています。古いメソッドはまったく動作しません。非同期実行のため、console.log( '5')は(3と4)より早く実行されます。非同期/待って書き換えがこれを与える:
だから、index.js:12 1
auth.js:4 2
auth.js:6 3
auth.js:7 undefined
auth.js:12 4
index.js:14 5
index.js:16 errorClass {isClientSafe: true, error: 404, reason: "Method 'auth.loginWithGoogle' not found", details: undefined, message: "Method 'auth.loginWithGoogle' not found [404]", …}
、ログから私は期待通りにそのコードが実行される見ることができます。 内部のauth.js:7私はエラー==未定義ですが、index.js(反応する部分)の中ではerrorClassです。
Meteorメソッドで非同期コードを扱うにはどうすればよいですか?
はインサイド私も非同期構文を使用し反応します。 –
@AshvinSharmaですが、loginWithGoogleメソッドはクライアントコンテキストが必要なため、クライアントサイドでコードを実行します。それとも間違っているのですか?) – yerassyl