nodejs - set variable inside switch statement then pass it to axios

797 Views Asked by At

I'm using inquirer.js to create a simple prompt for a CLI script. I'm trying to set a variable value inside a switch statement then use this variariable inside an axios instance configuration.

I'm facing the problem that the variable will be always undefined, how I can set it correctly?

let url;
    switch( answers.language ){
        case 'Italian': 
            url = 'https://it.wikipedia.org/w/api.php';
            break;
        case 'English': 
            url = 'https://en.wikipedia.org/w/api.php';
            break;
        case 'Deutch':
            url = 'https://de.wikipedia.org/w/api.php';
            break;
    }

    axios({
        url: url,
        method: 'GET',
        params: {
            action: 'query',
            list: 'search',
            srsearch: answers.search,
            format: 'json'
        }
    }).then( (response) => {
        console.log(response);
        console.log(response.data);
    });
1

There are 1 best solutions below

1
On

You need to add a default option in your switch case. for example:

switch(type)
{
    case 1:
        //something
    case 2:
        //something else
    default:
        // default option.
}