2017-08-02 22 views
1

我正在BlueMix OpenWHisk中開發一個模塊,在Cloudant提要更改後,我需要調用一個url,它將更新另一個平臺上的一些細節。我正在使用nodejs運行時。問題與回調 - 使用Nodejs運行時OpenWhisk

問題是我的操作等待POST請求的結果到上面提到的URL。如果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

我建議你閱讀一下Promises如何工作。我會稍微更新原始答案。 – 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 => {})的正面和負面的情況下分別 試試看吧...