0

For several days I am struggling in getting Azure Function in Node.js to work with external API. I tried different combinations and libraries like Axios or https, using.then as well as Async Await patterns and in every time during the execution I am getting only Promises in return. I suppose the function gets executed, but I am loosing its context in the main function. Even executing this simple example:

module.exports = async function (context, req) {
context.log('Before the call');
let request_options = {
        method: 'GET',
        host: 'https://jsonplaceholder.typicode.com',
        path: '/todos/1',
        headers: {
            'Content-Type': 'application/json'
        }
};
require('http')
    .request(
         request_options,
         function (res,context) {
               context.log(res);
               context.log('I am within response');
         });
context.log('After the call');
};

Results with:

2019-11-07T13:19:16.169 [Information] Executing 'Functions.Asyncawaittest' (Reason='This function was programmatically called via the host APIs.', Id=769ceae0-257f-432c-af8f-e35504ebc79a)
2019-11-07T13:19:20.880 [Information] Before the call
2019-11-07T13:19:20.881 [Information] After the call

Which indicates that a response is not handled by the context.log context. What am I missing to be able to debug the code within the response? Is it at all executed? I will have some nested API calls to deal with so it will get more complex to debug.

2
  • Do you get a response if you go to https://jsonplaceholder.typicode.com/todos/1 in your browser? Commented Nov 7, 2019 at 14:24
  • Actually in the mean time I set up my own API endpoint with node-red and ngrok to make it public and found out it fires in that direction. Then I found another related issue here on Stackoverflow on mixing promises with callbacks. So I dropped "async" from the model.exports function declaration and it works now and debugs to logs perfectly. thanks. Commented Nov 8, 2019 at 10:32

1 Answer 1

1

Actually in the mean time I set up my own API endpoint with node-red and ngrok to make it public and found out it fires in that direction. Then I found another related issue here on Stackoverflow on mixing promises with callbacks. So I dropped "async" from the model.exports function declaration and it works now and debugs to logs perfectly. thanks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.