1

while developing the cloud function I got the following error.

i created a cloud action and used that action name inside the json response in watson assistance. then once the intent is matched it call the cloud function. I selected nodejs as developing language of the cloud function.

now my real requirement is to call a https get request from that cloud function. for that i need to use nodejs library called node-rest-client.

normally we need to install it by typing "npm install node-rest-client" in the terminal. since there is no terminal in IBM nodejs editor i got, could you please let me know how to do that.

As a substitute i used https library which is already in nodejs package. and wrote the following code in the editor

const https = require('https');

function main() {
    https.get('https://f6054382.ngrok.io/webhook/testRequest', (resp) => {
        resp.on('data', (d) => {
            process.stdout.write(d);
        });
    });
}

main();

once I type "node init" in local folder and add the above code to index.js file and then run it using the command "node index.js" code works successfully by giving me the expected output from the webservice.

But I do not get that result in IBM nodejs editor

https://console.bluemix.net/openwhisk/details/action/dialogif.psi%2540gmail.com_dev/webhookCall/code

once i save the above code and invoke by clicking the button in right upper i get the successes response in green as follows.

Activation ID: 341d4e5bc81f4e489d4e5bc81f2e4888 Results: {} Logs: []

could you please help me to sort this out

Thank you

Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37
Dhanushka Sampath
  • 217
  • 1
  • 5
  • 17

2 Answers2

1

When executing an asynchronous operation, e.g. HTTP request, you need to return a Promise from the action handler. This ensures the platform will block on that asynchronous result before completing the invocation.

function main() {
    return new Promise((resolve, reject) => {
        https.get('https://f6054382.ngrok.io/webhook/testRequest', (resp) => {
        resp.on('data', (d) => {
            process.stdout.write(d);
            resolve({})
        });
    });
  })        
}
James Thomas
  • 4,303
  • 1
  • 20
  • 26
0

The underlying issue is that your code runs asynchronously and does not return a Promise to indicate a result will be available in the future.

I'd suggest to use the request-promises, which comes pre-packaged with OpenWhisk's Node.js runtime.

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

function main(params) {
    return request("https://f6054382.ngrok.io/webhook/testRequest").then(response => {
        process.stdout.write(response);
    });
}
markusthoemmes
  • 3,080
  • 14
  • 23