javascript - 无法将日期打印到 json 文件

标签 javascript node.js json

我正在编写代码以将日期直接从index.js打印到data.json,但收到数据未定义错误。

我做到了:

npm init

npm i jsonfile

index.js

const jsonfile = require('jsonfile');
const moment = require('moment');
       
const FILE_PATH = './data.json';

const DATE = moment().format();

const date = {

   date: DATE
 }

 jsonfile.writeFile(FILE_PATH, data);    //Error(here)
                              
 //ReferenceError: data is not defined

最佳答案

您有简单的拼写错误,只需更改日期而不是数据

const date = {

   date: DATE
}

jsonfile.writeFile(FILE_PATH, date); 

关于javascript - 无法将日期打印到 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58190079/

相关文章:

node.js - 如何使用nodejs和ibm_db包从DB2中的表中获取最后一个插入ID

Json 架构验证 : do not allow fields other than those declared in schema

java - 从JSONarray中获取具体值

javascript - 访问 Nativescript 布局中嵌套转发器中父 ListView 的索引

javascript - 如何使用 jQuery DataTables 从所有页面提交复选框

Javascript 函数在 if 语句中应该为 true 时返回 false?

node.js - Yarn 警告 : myCMS@0. 0.1:无许可证字段和许可证类型以及依赖项冲突

css - 如何从命令行运行 cssnano