Axios Azure function POST throwing error in NODEJS

2.2k Views Asked by At

I am using the following Code, Which throws error as shown below

Executed 'Functions.TranslateFunction' (Failed, Id=4cc27692-14a1-4c22-b0b7-7615d9812950) [12/10/2019 10:40:06 AM] System.Private.CoreLib: Exception while executing function: Functions.TranslateFunction. System.Private.CoreLib: Result: Failure Exception: TypeError: context.error is not a function Stack: TypeError: context.error is not a function at module.exports (C:\Users\M1056438\Coding\Functions\TranslateFunction\index.js:26:13) at processTicksAndRejections (internal/process/task_queues.js:93:5).

const axios = require('axios');
module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.name || (req.body && req.body.name)) {

// Make a QNA maker API call
var postData = {
    question: "Who are you"
  };
  let axiosConfig = {
    headers: {
        'Content-Type': 'application/json',
        "Authorization":"EndpointKey ********"
    }
  };

  try {
    const response =  await axios.post('https://qnamakercommonfordemos.azurewebsites.net/qnamaker/knowledgebases/********/generateAnswer', postData,axiosConfig)
    context.log(`statusCode: ${response.statusCode}`);
    context.log(response);
    context.done();
    return response; // or return a custom object using properties from response
  } catch (error) {
    // If the promise rejects, an error will be thrown and caught here
    context.error(error);
  }


        context.res = {
            // status: 200, /* Defaults to 200 */
            body: response
        };
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
};
1

There are 1 best solutions below

0
On

I think there is no function error defined on context object. But on context.log, additional logging methods are available that let you write function logs at other trace levels:

enter image description here

for Example:

context.log.info({hello: 'world'});

context.log.error("An error has occurred.");

For more details on 'context' object read here:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-node#context-object

So you can change context.error to context.log.error