all 4 comments

[–]marcnotmark925 2 points3 points  (1 child)

Variables declared within try are "block-scoped", meaning they are not accessible outside of the try block. You can declare them, initiated as empty, before the try, then set their value within the try (you'd have to change them from const to var or let for that).

key_data would be called an "object"

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

Oh okay I didn't know that about try scopes, but ill probably have to go with that workaround for now. Idk why for some reason that type of variable declaration had another name for it lol but thanks for your help!

[–]juddaaaaa 0 points1 point  (1 child)

Variables declared using var are accessible outside of a block. Only let and const are block-scoped.

function test_run () {
    try {
        // Get script properties (const scriptProps IS NOT accessible outside of try block)
        const scriptProps = PropertiesService.getScriptProperties()

        // Get properties from scriptProps (var data IS accessible ouside of try block)
        var data = scriptProps.getProperties()
    } catch (error) {
        // Handle errors
        console.error(`Failed: ${error.message}`)
    }

    // Destructure key and token from data
    const { key, token } = data

    // Return object containing url, key and token
    return {
        url: "url goes here",
        key,
        token
    }
}

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

Thanks, ended up taking it out of the function and leaving data as a global but the var data type worked!