locked
Async Javascript Function Returning 204 Instead of 200 With JSON Response RRS feed

  • Question

  • I can't get this function to return anything other than 204. Am I missing something?

    //function.json
    {
      "bindings": [
        {
          "authLevel": "anonymous",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
            "get",
            "post"
          ]
        },
        {
          "type": "http",
          "direction": "out",
          "name": "$return"
        }
      ]
    }

    //Function index.js
    let successStatus = false;
    
    module.exports = async function (context, req) {
      let log = context.log
      await getList(log).then(() => {
        if (successStatus) {
          return {
            status: 200,
            body: { "status": "Success" },
            headers: {
              'Content-Type': 'application/json'
            }
          }
        } else {
          return {
            status: 200,
            body: { "status": "Failed" },
            headers: {
              'Content-Type': 'application/json'
            }
          }
        }
      })
    };

    Thursday, December 5, 2019 4:52 PM

Answers

  • Though you are returning the response object in the then part, you are not really returning it in the function. You should return the awaited getList(log).then(...) too.

    Something like this

    //Function index.js
    let successStatus = false;
    
    module.exports = async function (context, req) {
      let log = context.log
      return await getList(log).then(() => {
        if (successStatus) {
          return {
            status: 200,
            body: { "status": "Success" },
            headers: {
              'Content-Type': 'application/json'
            }
          }
        } else {
          return {
            status: 200,
            body: { "status": "Failed" },
            headers: {
              'Content-Type': 'application/json'
            }
          }
        }
      })
    };

    Friday, December 6, 2019 4:55 AM

All replies

  • Though you are returning the response object in the then part, you are not really returning it in the function. You should return the awaited getList(log).then(...) too.

    Something like this

    //Function index.js
    let successStatus = false;
    
    module.exports = async function (context, req) {
      let log = context.log
      return await getList(log).then(() => {
        if (successStatus) {
          return {
            status: 200,
            body: { "status": "Success" },
            headers: {
              'Content-Type': 'application/json'
            }
          }
        } else {
          return {
            status: 200,
            body: { "status": "Failed" },
            headers: {
              'Content-Type': 'application/json'
            }
          }
        }
      })
    };

    Friday, December 6, 2019 4:55 AM
  • Thank you!
    Friday, December 6, 2019 11:27 AM