我如何一次使用多个询问者进行JavaScript
inquirer
.prompt([
{
name: 'name',
message: 'Enter team members name.',
},
{
name: 'role',
type: 'list',
message: 'Enter a team members role',
choices: [
'Engineer',
'Manager',
'Intern'
],
},
{
name: 'id',
message: 'Please enter team members id',
type: 'input'
},
{
name: 'email',
message: 'Please enter team members email'
}
])
.then(function(data){
let moreRole = '';
if (data.role === 'Engineer') {
moreRole = 'Github username.'
}else if(data.role === 'Intern') {
moreRole = 'school name.'
}else {
moreRole = 'office Number.'
}
})
inquirer
.prompt([
{
message: `Enter team members ${moreRole}`,
name: 'moreRole'
},
{
type: 'list',
message: 'Would you like to add another team member?',
choices: [
'Yes',
'No'
],
name: 'anotherMember'
}
])
.then(moredata => {
console.log(moredata)
});
因此,第一个询问者可以工作,但是当我添加第二个调查器并运行它时。它无法正常工作。我不能使用两个询问者背对背吗?我将如何工作。在控制台中,它只是问我第一个问题,然后回去,不允许我回答。
inquirer
.prompt([
{
name: 'name',
message: 'Enter team members name.',
},
{
name: 'role',
type: 'list',
message: 'Enter a team members role',
choices: [
'Engineer',
'Manager',
'Intern'
],
},
{
name: 'id',
message: 'Please enter team members id',
type: 'input'
},
{
name: 'email',
message: 'Please enter team members email'
}
])
.then(function(data){
let moreRole = '';
if (data.role === 'Engineer') {
moreRole = 'Github username.'
}else if(data.role === 'Intern') {
moreRole = 'school name.'
}else {
moreRole = 'office Number.'
}
})
inquirer
.prompt([
{
message: `Enter team members ${moreRole}`,
name: 'moreRole'
},
{
type: 'list',
message: 'Would you like to add another team member?',
choices: [
'Yes',
'No'
],
name: 'anotherMember'
}
])
.then(moredata => {
console.log(moredata)
});
So the first inquirer works but when I add the second one and run it. It doesn't work properly. Can I not use two inquirers back to back? How would I get it to work. In the console it just asks me the first question and and goes back out and doesn't let me answer it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
包装的API基于承诺。第一个
询问器.prompt
呼叫启动第一个提示,但是由于您使用的是然后,然后
处理已解决的承诺的方法,因此它不在等待操作完成通向第二个询问器。
要绕过这一点,您可以将代码包装到
async
函数和预处等待到每个询问器。最后一个)。
应该看起来像这样的东西:
如果您想对此事有更多见解,请检查文档是否承诺,事件循环,异步/等待。
The API of the package is based on Promises. The first
inquirer.prompt
call fires the first prompt, but since you are using thethen
way of handling resolved Promises, it is not waiting for the operation to complete, it just goes through to the secondinquirer.prompt
call.To bypass this, you could wrap your code into an
async
function and prependawait
to eachinquirer.prompt
(or at least to all but the last one).Should look something like this:
Check docs for Promises, event loop, async/await if you want more insight on this matter.