Upstate NY
Upstate NY

Reputation: 1

How to pass JSON() result to another function?

I'm having trouble passing an axios result to another function, which takes the argument for a more detailed axios.get.

The goal is to get a memberId, then pass that to another function that calls axios() for member details.

Here is my code:

async function membersGet() {

    var id;

    const members = 'http://legislation.nysenate.gov/api/3/members/2020?mykey'
    const config = {
        method: 'get',
        url: members
    }

    let res = await axios(config)

    let number = 0;
    do {
        number += 1;
        id = console.log(res.data.result.items[number].memberId);
        memberGet(id);
       } while (number < 5);
}

async function memberGet(mem) {

    const memberUrl = 'http://legislation.nysenate.gov/api/3/members/2013/[mem]?key=mykey'

    const config = {
        method: 'get',
        url: memberUrl
    }

    let res = await axios(config)

    var chamber = res.data.result.chamber
    var name = res.data.result.fullName
    var district = res.data.result.districtCode
    var mail = res.data.result.imgName

    console.log(chamber);
    console.log(name);
    console.log(district);
    console.log(mail);



   
}

When I try this I get this error: "UnhandledPromiseRejectionWarning: Error: Request failed with status code 400"

What am I missing?

Upvotes: 0

Views: 30

Answers (1)

Bishan
Bishan

Reputation: 15740

Update you code as below.

let res = await axios(config)

let number = 0;
do {
    number += 1;
    id = res.data.result.items[number].memberId;
    console.log('ID is : ',id);
    memberGet(id);
   } while (number < 5);

Upvotes: 1

Related Questions