javascript - 无法在 node.js 中执行承诺
问题描述
我对 node.js 完全陌生,我想做的就是使用查询器 NPM 提出几个问题,将这些问题的答案插入模板文字并从中创建一个 readme.md 文件。出于某种原因,我的代码不起作用。有人能指出我在这里做错了什么吗?我觉得在创建承诺时我可能缺少一些参数??
const inquirer = require("inquirer")
const fs = require("fs")
const util = require("util")
const readme = () => {
return `
# ${data.title}
License covered under ${data.license}
## Table of Contents
1. [Description](#description)
2. [Installation](#installation)
3. [Usage](#usage)
4. [License](#license)
5. [Contributing](#contributing)
6. [Test](#test)
7. [Question](#question)
## Description
${data.description}
## Installation
${data.installation}
## Usage
\```
${data.usage}
\```
## License
${license}
## Contributing
Accepting contribution: ${data.contribution}
${data.contributionRequirement}
## Tests
${data.test}
## Questions
- Github Repo :
- Email : ${data.email} `
}
// array of questions for user
const questions = [
{
type: "input",
name: "title",
message: "What is the title of your project",
default: "Not Available"
},
{
type: "input",
name: "username",
message: "What is your Github username?",
default: "Not Available"
},
{
type: "input",
name: "email",
message: "What is your email address?",
default: "Not Available"
},
{
type: "input",
name: "contact",
message: "How do you want people to reach you?",
default: "Not Available"
},
{
type: "input",
name: "description",
message: "Please type a short description of your project ( When you are done, hit ESC & type \":wq\" to exit editor )",
default: "Not Available"
},
{
type: "input",
name: "installation",
message: "How do you install this application? ( When you are done, hit ESC type \":wq\" to exit editor) ",
default: "Not Available"
},
{
type: "input",
name: "usage",
message: "Give example of some ways you can use this application ( When you are done, hit ESC & type \":wq\" to exit editor)",
default: "Not Available"
},
{
type: "list",
name: "license",
message: "What kind of license would you like to use?",
choices: ["MIT","Microsoft Public License","Mozilla Public License 2.0","Academic Free License v3.0","Open Software License 3.0","Creative Commons Attribution 4.0"],
default: "Not Available"
},
{
type: "confirm",
name: "contribution",
message: "Are you open to contribution?",
default: "Not Available"
},
{
type: "input",
name: "contributionRequirement",
message: "If Yes, What are your requirement for giving contribution?",
default: "Not Available"
},
{
type: "input",
name: "test",
message: "Please give instructions for testing of this project ( When you are done, hit ESC & type \":wq\" to exit editor)",
default: "Not Available"
}
]
// function to write README file
function writeToFile(fileName, data) {
fs.writeFile(fileName, data, "utf8", function(err) {
if (err) {
console.log(err);}
})}
// function to initialize program
const init = new Promise(function(resolve,reject) {
resolve(inquirer.prompt(questions))
})
// function call to initialize program
init.then(writeToFile("test.md",readme)
解决方案
readme
inquirer
获得所有输入后应该运行结果。
const inquirer = require ('inquirer');
const fs = require ('fs');
const util = require ('util');
const readme = data => {
return `
# ${data.title}
License covered under ${data.license}
## Table of Contents
1. [Description](#description)
2. [Installation](#installation)
3. [Usage](#usage)
4. [License](#license)
5. [Contributing](#contributing)
6. [Test](#test)
7. [Question](#question)
## Description
${data.description}
## Installation
${data.installation}
## Usage
\`\`\`
${data.usage}
\`\`\`
## License
${data.license}
## Contributing
Accepting contribution: ${data.contribution}
${data.contributionRequirement}
## Tests
${data.test}
## Questions
- Github Repo :
- Email : ${data.email} `;
};
// array of questions for user
const questions = [
{
type: 'input',
name: 'title',
message: 'What is the title of your project',
default: 'Not Available',
},
{
type: 'input',
name: 'username',
message: 'What is your Github username?',
default: 'Not Available',
},
{
type: 'input',
name: 'email',
message: 'What is your email address?',
default: 'Not Available',
},
{
type: 'input',
name: 'contact',
message: 'How do you want people to reach you?',
default: 'Not Available',
},
{
type: 'input',
name: 'description',
message: 'Please type a short description of your project ( When you are done, hit ESC & type ":wq" to exit editor )',
default: 'Not Available',
},
{
type: 'input',
name: 'installation',
message: 'How do you install this application? ( When you are done, hit ESC type ":wq" to exit editor) ',
default: 'Not Available',
},
{
type: 'input',
name: 'usage',
message: 'Give example of some ways you can use this application ( When you are done, hit ESC & type ":wq" to exit editor)',
default: 'Not Available',
},
{
type: 'list',
name: 'license',
message: 'What kind of license would you like to use?',
choices: [
'MIT',
'Microsoft Public License',
'Mozilla Public License 2.0',
'Academic Free License v3.0',
'Open Software License 3.0',
'Creative Commons Attribution 4.0',
],
default: 'Not Available',
},
{
type: 'confirm',
name: 'contribution',
message: 'Are you open to contribution?',
default: 'Not Available',
},
{
type: 'input',
name: 'contributionRequirement',
message: 'If Yes, What are your requirement for giving contribution?',
default: 'Not Available',
},
{
type: 'input',
name: 'test',
message: 'Please give instructions for testing of this project ( When you are done, hit ESC & type ":wq" to exit editor)',
default: 'Not Available',
},
];
// function to write README file
function writeToFile (fileName, data) {
fs.writeFile (fileName, data, 'utf8', function (err) {
if (err) {
console.log (err);
}
});
}
// function call to initialize program
inquirer.prompt(questions).then(answer => {
writeToFile('test.md', readme(answer))
});
推荐阅读
- node.js - 是否有任何特殊原因或情况我无法向 Mongoose Schema 添加新字段?
- c++ - 在 *modern* C++ 中,我应该如何管理 *unowned* 指针?
- local-storage - 如何在 UserDefaults 中存储自定义对象?
- r - 自动将 R Markdown 应用程序重定向到不同的链接
- jenkins - Jenkins共享库,同一个类的多个版本
- c# - ASP.Net Core Web API 如何调用存储过程并返回 1 个结果对象
- sql - 为 BigQuery 中的每一天定义虚拟列
- javascript - 节点 js 和图表 js 的 ejs 对象存在问题
- ios - 如何在 Firestore 中获取文档密钥
- ruby-on-rails - 使用 mysql 数据库设计可跟踪的迁移错误