2017-11-23 8 views
-1

誰かが助けることを願っています。 私はマジックミラーの翻訳部分に取り組んでいます。私は天気MODULの内部node_helper.jsを使用します。私はこの約束のもので狂っている

  if (this.config.lang != 'en'){ 
       console.log("in Translate"); 
        translate(alerts.description, {to: this.config.lang}).then((res) => {console.log(res.text); return res;}); 
        translate(alerts.expires, {to: this.config.lang}).then(res => {console.log(res.text)}); 
       translate(alerts.message, {to: this.config.lang}).then(res => {console.log(res.text)}); 
      } 

など:私はuのにコードをお見せしましょう。 config.logを調べると、私が見たいと思っているすべての結果が得られますが、次のコードでそれらを使用できません。

+0

、翻訳機能は、あなたが助けたい問題は何であるの約束 –

+0

を翻訳して返しGoogleからのでしょうか? – jfriend00

+0

私が言ったように、translateの出力はconsole.logに正しく表示されていますが、3つの翻訳通話の後でコードに必要です。これは私の問題です –

答えて

0

Promise.all()を使用すると、約束を返す3つの操作行われ、その結果の.then()コールバックでそれらの結果の使用を使用している:ところで

 if (this.config.lang != 'en'){ 
      console.log("in Translate"); 
      Promise.all([ 
       translate(alerts.description, {to: this.config.lang}), 
       translate(alerts.expires, {to: this.config.lang}), 
       translate(alerts.message, {to: this.config.lang}) 
      ]).then(function(results) { 
       // results[0], results[1], results[2] contain the three results 
       // use those three values in the code inside this callback 
      }) 
     } 
+0

助けてくれてありがとうございます。私は、ur codeを試して、console.log( "x ="、result [0]); x = [オブジェクトオブジェクト] –

+0

@RobertWeigerstorfer - 試した実際のコードを表示しなければなりません。私の例では、あなたのコメントに表示されているように、 'result [0]'ではなく 'results [0]'を使います。 – jfriend00

+0

本当にありがとうございます結果がjson形式なので、結果[0] .textを入力すると正しい返信が得られたと言って忘れました。 –

関連する問題