简体   繁体   中英

azure basic HTTP trigger NodeJS async

Why is this not sending me back any data. Im using postman and its not sending any results. Some type of async issue? Maybe I didn't configure this Azure HTTP trigger functions.json config correctly?

When I pass in a local object for the response it is fine. Only when I attempt to use async its failing. I'm currently testing on my local dev machine before I deploy the function to Azure cloud.

functions.json

{
  "disabled": false,
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

index.js

const axios = require('axios');

module.exports = async function (context, req) {
    axios.get('https://jsonplaceholder.typicode.com/todos/1').then(response=>{
        console.log(response.data) // This is coming thru fine.
        context.res = {
            status: 200,
            body: response.data
        }
        context.done();
    }).catch(error=>{
        context.res = {
            status: 400,
            body: error
        }
        context.done();
    })
}

I honestly believe that the code I had before works but the more condensed version I provided below seem to do the job however if anyone runs into this issue you may want to try to restart the local development server and it may resolve the issue for you

SOLVED

const axios = require(‘axios’);

module.exports = async function (context, req) {
    let d = await axios.get(‘https://jsonplaceholder.typicode.com/todos/1’);
    context.res = {
        status: 200,
        body: d.data
    }
    context.done()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM