javascript - 无法创建 notes.json 文件
问题描述
这是app.js
代码:
const chalk = require('chalk');
const validator = require('validator');
const yargs = require('yargs');
const notes = require('./notes.js');
yargs.version('1.1.0');
yargs.command[{
command: 'add',
describe: 'Add a New Note',
builder: {
title: {
describe: 'Note Title',
demandOption: true,
type: 'string'
},
body: {
describe: 'the body of the note',
demandOption: true,
type: 'string'
}
},
handler: function (argv) {
notes.addNote(argv.title, argv.body)
}
}]
yargs.parse();
文件notes.js
:
const fs = require('fs');
const getNotes = function() {
}
const addNote = function(title, body) {
const notes = loadNotes();
notes.push({
title: title,
body: body
});
savedNotes(notes);
}
const savedNotes = function(notes) {
const dataJson = JSON.stringify(notes);
fs.writeFileSync('notes.json',dataJson);
}
const loadNotes = function() {
try {
const dataBuffer = fs.readFileSync('notes.json');
const dataJson = dataBuffer.toString();
return JSON.parse(dataJson);
} catch(e) {
return {};
}
}
module.exports = {
getNotes: getNotes,
addNote: addNote
}
但是,我仍然无法notes.json
通过 .push 创建文件fs.writeFileSync()
,并且如果在显示错误后放置一个圆括号yargs.command
,说明 .push 不是我从notes.js
文件中使用的函数
解决方案
推荐阅读
- sql - 什么是 SQL 中的脏读?
- c# - 如何从存储在数组中的字符串中获取连续的相同字符集?例如' INPUT: Sword|word|cord 输出: ord
- android - 我不明白如何将参数从活动传递到自定义视图
- sql - 将数据从一个表移动到另一个 SQL Server 主键约束
- r - 如何手动构建ggplot2色标?
- c# - 我在使用托管服务标识对使用 AAD 保护的 Azure 应用服务进行身份验证时遇到问题
- node.js - Node JS - Firestore get() 响应永远不会被调用
- python - 如何遍历交换机的所有端口并使用 python 和 ssh 将它们一一禁用?
- r - 在匹配或 NA 列上左连接 data.tables
- github - 如何向 GitHub GraphQL API 发出未经身份验证的请求