1

I was wondering how I can access the response data in the second funtion getJobId(), since the first one works fine. I looked around here but others use-case was a bit different and despite managing to get the getToken() function to work I don't fully grasp what I have to change for the getJobId() function.

$("#route-optimize").click(async function() {
    var token = await getToken();
    console.log(token); // Returns the token
    var jobId = await getJobId(token);
    console.log(jobId); // undefined
});

async function getToken(){
    try {
        const res = await axios.post('https://www.arcgis.com/sharing/rest/oauth2/token?client_id=abc&client_secret=dfe&grant_type=client_credentials&expiration=20100');
        return res.data.access_token;
    } catch (e) {
        return 'ERROR';
    }
}

async function getJobId(token){
    var data = {
        'f': 'json',
        'token': token,
        'populate_directions': 'true',
        'uturn_policy': 'ALLOW_UTURNS',
        'depots': '{\n  "type":"features",\n  "features" : [{\n    "attributes" : {\n        "Name" : "Bay Cities Kitchens & Appliances"\n    },\n    "geometry" : {\n        "x" : -118.469630,\n        "y" : 34.037555\n    }\n  }]\n}',
        'routes': '{\n  "features": [{\n      "attributes": {\n          "Name": "Route 1",\n          "Description": "vehicle 1",\n          "StartDepotName": "Bay Cities Kitchens & Appliances",\n          "EndDepotName": "Bay Cities Kitchens & Appliances",\n          "Capacities": "4",\n          "MaxOrderCount": 3,\n          "MaxTotalTime": 60,\n        }\n      },\n      {\n    "attributes": {\n          "Name": "Route 2",\n          "Description": "vehicle 2",\n          "StartDepotName": "Bay Cities Kitchens & Appliances",\n          "EndDepotName": "Bay Cities Kitchens & Appliances",\n          "Capacities": "4",\n          "MaxOrderCount": 3,\n          "MaxTotalTime": 60,\n        }\n      }\n  ]\n}',
        'orders': '{\n  "type": "features",\n  "features": [{\n      "attributes": {\n          "Name": "Father\'s Office",\n          "ServiceTime": 10\n      },\n      "geometry": {\n          "x": -118.498406,\n          "y": 34.029445\n      }\n  },\n  {\n      "attributes": {\n          "Name": "R+D Kitchen",\n          "ServiceTime": 10\n      },\n      "geometry": {\n          "x": -118.495788,\n          "y": 34.032339\n      }\n  },\n  {\n      "attributes": {\n          "Name": "Pono Burger",\n          "ServiceTime": 10\n      },\n        "geometry": {\n        "x": -118.489469,\n        "y": 34.019000\n      }\n    },\n  {\n      "attributes": {\n          "Name": "Il Ristorante di Giorgio Baldi",\n          "ServiceTime": 10\n      },\n        "geometry": {\n        "x": -118.518787,\n        "y": 34.028508\n      }\n    },\n  {\n      "attributes": {\n          "Name": "Milo + Olive",\n          "ServiceTime": 10\n      },\n        "geometry": {\n        "x": -118.476026,\n        "y": 34.037572\n      }\n    },\n  {\n      "attributes": {\n          "Name": "Dialogue",\n          "ServiceTime": 10\n      },\n        "geometry": {\n        "x": -118.495814,\n        "y": 34.017042\n      }\n    }\n  ]\n}\n',
        'default_date': '1523664000000'
    }
    var config = {
        method: 'post',
        url: 'https://logistics.arcgis.com/arcgis/rest/services/World/VehicleRoutingProblem/GPServer/SolveVehicleRoutingProblem/submitJob',
        headers: { 
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data : new URLSearchParams(data).toString()
    };
    axios(config).then(function (response) {
        return response.data.jobId;
    }).catch(function (error) {
        console.log(error);
    });
}
Andrew Fox
  • 69
  • 1
  • 7
  • Can you `console.log(response)` before `return response.data.jobId;` and see if it has required data ? – wak786 Sep 30 '20 at 19:18

1 Answers1

2

Since axios(config) returns a promise, you can await for its response then return the jobId like you did in the other function :

async function getJobId(token){
    //...
    const response = await axios(config);
    return response.data.jobId;
}
Hassan
  • 609
  • 4
  • 9