【问题标题】:How can I get a value out of inquirer.js?如何从inquirer.js 中获取价值?
【发布时间】:2018-08-20 01:14:52
【问题描述】:

我正在使用 javascript、yargs、inquirer 和 superagent 构建一个小型 CLI 应用程序。在查询器中,我要求用户输入要在我的应用程序中使用的里程选择。我想在我的应用程序的其他地方使用该值,但我似乎无法获得返回的值。下面是我最近的尝试。对于从 selectRange 返回此值的任何帮助,我们将不胜感激。

const selectRange = (result) => {
    return inquirer.prompt([{
        type: 'checkbox',
        message: 'Select the range in miles to search',
        name: 'miles',
        choices: ['50', '100','150', '200', '250'] ,
        validate: (result) => {
            if (result.length > 1) {

                return 'Error: You must select 1 choice     only'

            } else {
                return true
            }
        },
        filter: input => {
            return input

        }

    }]).then(input => {
        return input
    })
}



const surroundingCitiesWeather = (location) => {

    const range = selectRange()

    console.log(`Range selected is ${range}`)
}

Here is a pic of my output, mind the last line

【问题讨论】:

    标签: javascript node.js inquirerjs


    【解决方案1】:

    你的函数返回一个 Promise,所以你需要使用它:

    const surroundingCitiesWeather = (location) => {
        selectRange().then(range => {
            console.log(`Range selected is ${range}`)
        })
    }
    

    如果你使用的是最新版本的 node,你可以使用 async/await 更清楚一点:

    const surroundingCitiesWeather = async (location) => {
        const { range } = await selectRange()
        console.log(`Range selected is ${range}`)
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-01
      • 2018-04-07
      • 2021-12-26
      • 1970-01-01
      • 2016-08-24
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多