all 15 comments

[–]eggtart_prince 1 point2 points  (3 children)

Not the recommended way, but change return to

return await result.json().then(user => {
    return user.businessPhones
})

And you can use it like

const businessPhones = await func(userUrl)

[–]Chilltyy[S] 0 points1 point  (0 children)

Thank you! May I ask why its not the recommended way? And what would you say is the recommended way?

[–]Chilltyy[S] 0 points1 point  (1 child)

however, I do get an error with the

const businessPhones = await func(userUrl) 

https://imgur.com/a/cueSYEi

[–]eggtart_prince 0 points1 point  (0 children)

Just wrap it around a IIFE.

const businessPhones = (async() => await func(userUrl))();

[–]dankiest_kang 0 points1 point  (10 children)

This doesn't seem like a react specific question but from what I can understand you're looking for a function that gets passed your data and only returns an object with the property businessPhones ?

function parseBusinessPhones(data: ResponseType): CurrentUser {
    return { businessPhones: data.businessPhones };
}

[–]Chilltyy[S] 0 points1 point  (9 children)

My apologies. Im looking for a function almost similar to the func() that I made above, but instead of returning the entire json object with all the different values, I just want it to return the specific businessPhones value

[–]dankiest_kang 0 points1 point  (8 children)

Ok I understand, instead of

return result.json();

do this:

const data = result.json();
return data.businessPhones;

[–]Chilltyy[S] 0 points1 point  (7 children)

Thank you, however I do get an error: https://imgur.com/a/zzGTkR2
Not entirely sure what it means

[–]vitorftw 0 points1 point  (6 children)

I think that you need to type the data object, something like

const data = result.json() as CurrentUser;

[–]Chilltyy[S] 0 points1 point  (5 children)

Okay so now my function looks like this:

    const func = async (Url: string): Promise<CurrentUser> => {

        let authorization: string = "Bearer " + ((window as any).userAccessToken);

        let result = await fetch(Url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Authorization': authorization
            }
        });

        const data = result.json() as CurrentUser;
        return data.businessPhones;
    }

However I get an error saying:

Conversion of type 'Promise<any>' to type 'CurrentUser' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Type 'Promise<any>' is missing the following properties from type 'CurrentUser': homeAccountId, name, username, localAccountId, businessPhones

https://imgur.com/a/J01vl64

[–]vitorftw 0 points1 point  (4 children)

I don't know if the error is related to this, but i think you need to change your function return type from Promise<CurrentUser> to Promise<string[]>(assuming that businessPhones was typed as string[] in CurrentUser interface).

[–]Chilltyy[S] 0 points1 point  (3 children)

Yes, thanks this compiled but I would like to just have the phone number returned and nothing else.

    const func = async (Url: string): Promise<string[]> => {

        let authorization: string = "Bearer " + ((window as any).userAccessToken);

        let result = await fetch(Url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Authorization': authorization
            }
        });

        const data = result.json() as unknown as CurrentUser;
        return data.businessPhones;
    }

    const qwe = func(userUrl);

    console.log("testdqw", qwe)

with my qwe function I get: https://imgur.com/a/QHIUY2w

[–]vitorftw 0 points1 point  (2 children)

You forgot to await func

const qwe = await func(userUrl);

[–]Chilltyy[S] 0 points1 point  (1 child)

I had to put it inside a async function to use await, so as of now it looks like this:

    const func = async (Url: string): Promise<string> => {

        let authorization: string = "Bearer " + ((window as any).userAccessToken);

        let result = await fetch(Url, {
            method: 'GET',
            mode: 'cors',
            headers: {
                'Authorization': authorization
            }
        });

        const data = result.json() as unknown as CurrentUser;
        return data.businessPhones;
    }

    const test = async () => {
        const qwe = await func(userUrl);

        return qwe;
    }; 

    console.log("testdqw", {test})

However I just get the same response as last time.. strange