如何解决在测试项目中从源json文件创建新的json
更新 我将继续研究此过程,并提供以下代码-st
async generatejobs() {
const fs = require("fs");
const path = require("path");
const directoryPath = path.join(__dirname,"../data/pjo/in");
fs.readdir(directoryPath,function (err,files) {
if (err) {
console.log("Error getting directory information." + err);
} else {
files.forEach(function (file) {
console.log(file);
fs.readFile(file,(err,data) => {
console.log(file); // this works,if I stop here
// if (err) throw err;
// let newJson = fs.readFileSync(data);
// console.log(newJson);
})
// let data = fs.readFileSync(file);
// let obj = JSON.parse(data);
// let isoUtc = new Date();
// let isoLocal = toISOLocal(isoUtc);
// obj.printingStart = isoLocal;
// obj.printingEnd = isoLocal;
// let updatedFile = JSON.stringify(obj);
// let write = fs.createWriteStream(
// path.join(__dirname,"../data/pjo/out",updatedFile)
// );
// read.pipe(write);
});
}
});
一旦我尝试取消注释下面显示的行,它就会失败。
let newJson = fs.readFileSync(data);
我得到的错误是这个。
Uncaught ENOENT: no such file or directory,open 'C:\projects\codeceptJs\ipt\80-012345.json'
这是一个正确的声明,因为路径应如下所示。
'C:\projects\codeceptJs\ipt\src\data\pjo\in\80-012345.json'
我不明白为什么要在这里查找文件,因为在代码的前面已经设置了路径,并且似乎可以通过此路径正确查找文件。
const directoryPath = path.join(__dirname,"../data/pjo/in");
我目前正在尝试执行以下操作的代码的其余部分。
- 从源目录中获取每个文件
- 放入json对象
- 更新json对象以更改两个日期条目
- 保存到我的项目中的新json文件/新位置
原始帖子 我有一个codeceptjs测试项目,想在我的项目(src / data / jsondata / in)中包含一组现有的json文件,然后更新每个项目中的date属性,并将它们写入我项目中的输出位置(src / data / jsondata / out)。我需要更改日期,然后将其重新设置为非常特定的字符串格式(已完成此操作),然后将其插入到正在创建的新json中。我大约有80%的问题都得到了解决,然后在尝试将文件从项目中的一个文件夹移至另一个文件夹时遇到了问题。
我将其分为两部分。
-
函数获取日期并将其转换为我需要的日期字符串
-
函数可获取源json,更新日期并在新文件夹位置创建新json
1号正在正常工作。 2号不是。
如果有更好的方法可以做到这一点,我对此持开放态度。
这是我尝试更新json的代码。这里的主要问题是我无法正确理解和/或正确处理联接路径。
generatePressJobs() {
//requiring path and fs modules
const path = require('path');
const fs = require('fs');
//joining path of directory
const directoryPath = path.join(__dirname,'../','data/pjo/in/');
//passsing directoryPath and callback function
fs.readdir(directoryPath,files) {
//handling error
if (err) {
I.say('unable to scan directory: ' + err);
return console.log('Unable to scan directory: ' + err);
}
//listing all files using forEach
files.forEach(function (file) {
// Update each file with new print dates
let data = fs.readFileSync(file);
let obj = JSON.parse(data);
let isoUtc = new Date();
let isoLocal = toISOLocal(isoUtc);
obj.printingStart = isoLocal;
obj.printingEnd = isoLocal;
let updatedFile = JSON.stringify(obj);
fs.writeFile(`C:\\projects\\csPptr\\ipt\\src\\data\\pjo\\out\\${file}`,updatedFile,(err) => {
if (err) {
throw err;
}
});
});
});
},
收到错误
Uncaught ENOENT: no such file or directory,open '80-003599.json'
at Object.openSync (fs.js:462:3)
at Object.readFileSync (fs.js:364:35)
at C:\projects\codeceptJs\ipt\src\pages\Base.js:86:23
at Array.forEach (<anonymous>)
at C:\projects\codeceptJs\ipt\src\pages\Base.js:84:13
at FSReqCallback.oncomplete (fs.js:156:23)
生成json的函数位于src / pages / basePage.js
我为json文件构建的文件夹结构位于 src / data / jsondata / in->用于原始源文件 src / data / jsondata / out->更改后的结果json
任何见解或建议将不胜感激。
谢谢, 鲍勃
解决方法
我的方法/解决方法 沿用我在事件中采取的最终方法,这对其他任何人都有帮助。中间的数据特定于我的要求,但是留给我展示了我完成所需工作的过程。
async generatePressjobs(count) {
const fs = require("fs");
const path = require("path");
const sourceDirectoryPath = path.join(__dirname,"../data/pjo/in/");
const destDirectoryPath = path.join(__dirname,"../data/pjo/out/");
for (i = 0; i < count; i++) {
// read file and make object
let content = JSON.parse(
fs.readFileSync(sourceDirectoryPath + "source.json")
);
// Get current date and convert to required format for json file
let isoUtc = new Date();
let isoLocal = await this.toISOLocal(isoUtc);
let fileNameTimeStamp = await this.getFileNameDate(isoUtc);
// Get current hour and minute for DPI time stamp
let dpiDate = new Date;
let hour = dpiDate.getHours();
let minute = dpiDate.getMinutes();
dpiStamp = hour + '' + minute;
// update attributes in the json obj
content.batchid = `80-0000${i}`;
content.id = `80-0000${i}-10035-tcard-${dpiStamp}-0101010000_.pdf`
content.name = `80-0000${i}-8.5x11CALJEF-CalBody-${dpiStamp}-01010100${i}_.pdf`;
content.printingStart = isoLocal;
content.printingEnd = isoLocal;
// write the file
fs.writeFileSync(
destDirectoryPath + `80-0000${i}-SOME-JOB-NAME-${dpiStamp}.pdf_Press Job printing end_${fileNameTimeStamp}.json`,JSON.stringify(content)
);
}
},
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。