2017-08-02 8 views
1

私はCloudMix OpenWHiskでモジュールを開発しています。ここでCloudantフィードを変更した後、URLを呼び出す必要があります。私はnodejsランタイムを使用しています。コールバックに関する問題 - NodejsランタイムでのOpenWhisk

私のアクションは、上記のURLにPOSTリクエストの結果を待つことです。 POSTが成功した場合は、次の一連のイベントを実行する必要があります。

質問:

  1. 次のシーケンスが実行される前に、POST要求の結果を待つ方法は?

  2. POSTリクエストの結果を待って戻すことは可能ですか?

は私のコードをポジショニング

/** 
    * 
    * main() will be invoked when you Run This Action 
    * 
    * @param OpenWhisk actions accept a single parameter, which must be a JSON object. 
    * 
    * @return The output of this action, which must be a JSON object. 
    * 
    */ 

const util = require('util'); 
var http = require('http'); 

function main(params) { 

    // Updated the status of the User 
    params.status ="updated1"; 

    var options = { 
       host: "myhost.mybluemix.net", 
       path: "/addtoimc", 
       method: "POST", 
       headers: { 
        "Content-Type": "text/plain" 
       } 
      }; 


     return {message : addtoIMC(options)}; 

} 

function createRequest(data, options) 
{ 

    return http.request(options, function (res) { 
      var responseString = ""; 

      res.on("data", function (data) { 
       responseString += data; 
       // save all the data from response 
      }); 
      res.on("end", function() { 
       console.log("AAA" + responseString); 
      }); 
     }); 
} 



function addtoIMC(options) 
{ 
    return new Promise(function(resolve, reject) { 
      var req = createRequest("Hello",options); 
      var reqBody = "post_data"; 
      req.write(reqBody); 
      req.end(); 

     }); 
} 

答えて

3

リクエストロジックが少し壊れています。例えば、あなたの約束は決して解決されず、正しいコールバックを聞いてはいけません。

request-promiseに切り替えることをおすすめします。次はあなたがこのような何か書くことができます

const request = require('request-promise'); 

function main(params) { 
    return request({ 
     url: "http://myhost.mybluemix.net/addtoimc", 
     method: "POST", 
     headers: { 
      "Content-Type": "text/plain" 
     } 
    }).then(response => { 
     if(response.success) { 
      return Promise.resolved({message: "nice"}); 
     } else { 
      return Promise.rejected({error: "it broke"}); 
     } 
    }); 
} 
+0

次のエラーが表示されます。{"error": "このアクションは辞書を返しませんでした。戻り値が{"success":false}の場合、POSTからの戻り値を追跡する必要があります。次のアクションを停止する必要があります。 – bukubapi

+0

プロミスの仕組みについて少しお読みになることをお勧めします。私は元の答えを少し更新します。 – markusthoemmes

0

動作する必要があります。それを試してみる

function main(params) { 
    const http = require('http'); 

    const inputVariable = params.inputVariableNameToBePassedToThisAction; 

    const options = { 
    host: "myhost.mybluemix.net", 
    path: "/addtoimc", 
    method: "POST", 
    headers: { 
     "Content-Type": "text/plain" 
    } 
    }; 

    const createRequest = (options) => { 
    const promise = new Promise((resolve, reject) =>{ 
     http.request(options, function(err, resp) { 
     if(err){ 
      reject(`401: ${err}`); 
     } 
     else{ 
      let responseString = ""; 

      res.on("data", function (data) { 
      responseString += data; 
      // save all the data from response 
      }); 
      res.on("end", function() { 
      console.log("AAA" + responseString); 
      }); 
      resolve(responseString); 
     } 
     }); 
    }); 
    return promise; 
    }; 

    return createRequest(options) 
    .then(data => { 
     //process data from POST request 
     //call next methods if there are any, on positive response of POST request 
     const outputVariable = data.someImportantParameterToExtract; 
     const resp = { 
     keyToUse : outputVariable 
     }; 
     // 
     return ({ 
     headers: { 
      'Content-Type': 'application/json' 
     }, 
     statusCode: 200, 
     body: new Buffer(JSON.stringify(resp)).toString('base64') 
     }); 
    }) 
    .catch(err => { 
     //stop execution because there is some error 
     return ({ 
     headers: { 
      'Content-Type': 'application/json' 
     }, 
     statusCode: 400, 
     body: new Buffer(JSON.stringify(err)).toString('base64') 
     }); 
    }); 
}; 

あなたは、最初の関数を記述し、それを呼び出すと、正と負のシナリオで.then(data => {}).catch(err => {})を使用することができ、それぞれ を...

関連する問題