gpt4 book ai didi

javascript - 如何从 Meteor 中的文件夹中读取所有 JSON 文件?

转载 作者:行者123 更新时间:2023-11-30 16:36:16 25 4
gpt4 key购买 nike

我想在我的 Meteor 应用程序中读取文件夹内的所有 json 文件。

我有以下结构:

/server  
-- /methods
-- -- file1.json
-- -- file2.json

我尝试使用以下代码读取所有 JSON 文件:

var fs = Npm.require('fs');
var path = Npm.require('path');
var base = path.resolve('.');

try {
var files = fs.readdirSync(base + '/methods/*.json');
console.log(files);
} catch (e) {
console.dir(e)
}

但这不起作用,它向我显示一个错误,指出该目录或文件不存在。

我是不是犯了什么错误?或者还有其他方法吗?

最佳答案

首先,请小心寻找 Meteor 的项目根目录,因为 path.resolve('.') 甚至 process.env.PWD 的结果可能改变不同的部署设置。

其次,fs.readdirSync(path) 的参数path需要一个目录。因此,正确的调用应该是 var files = fs.readdirSync(base + '/server/methods/');

但是,我建议使用 Assets .只需将您的 JSON 文件移动到您的 private 目录,然后通过 Assets.getText(assetPath, [asyncCallback]) 在服务器上访问它们或 Assets.getBinary(assetPath, [asyncCallback]) .

例如:

if (Meteor.isServer) {
Meteor.startup(function() {
var example1 = JSON.parse(Assets.getText('methods/example1.json'));
var example2 = JSON.parse(Assets.getText('methods/example2.json'));
console.log(example1);
console.log(example2);
});
}

如果您想读取所有 JSON 文件,您可能需要以下解决方法:

if (Meteor.isServer) {
Meteor.startup(function() {
var exec = Npm.require('child_process').exec;
var files = [],
fileNames = [];
exec('ls -m assets/app/methods | tr -d \' \n\' ', Meteor.bindEnvironment(function(error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
fileNames = stdout.split(',');
/* Print all file names. */
console.log("File names:");
console.log(fileNames);
_.each(fileNames, function(fileName) {
/* Check if file has proper extension. */
if (fileName.split('.').pop() == 'json') files.push(JSON.parse(Assets.getText('methods/' + fileName)));
});
/* Print all JSON files. */
_.each(files, function(file) {
console.log(file);
});
}));
});
}

如果你想同步调用exec,你可能需要使用Meteor.wrapAsync(func, [context]) :

if (Meteor.isServer) {
var exec = Npm.require('child_process').exec;
var files = [], fileNames = [];

var execAsync = function (options, callback) {
console.log("execAsync()");
exec('ls -m assets/app/methods | tr -d \' \n\' ', Meteor.bindEnvironment(function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
fileNames = stdout.split(',');
/* Print all file names. */
console.log("File names:");
console.log(fileNames);
_.each(fileNames, function (fileName) {
/* Check if file has proper extension. */
if (fileName.split('.').pop() == 'json') files.push(JSON.parse(Assets.getText('methods/' + fileName)));
});
callback(null, options.callback);
}));
};

function postProcessing(callback) {
console.log("postProcessing()");
/* Print all JSON files. */
_.each(files, function (file) {
console.log(file);
});
callback();
}

Meteor.startup(function () {
/* Wrap asynchronous exec function, in order to call it in a synchronous style. */
var execSync = Meteor.wrapAsync(execAsync);
var refToPostProcessing = execSync({callback: postProcessing});
var postProcessingSync = Meteor.wrapAsync(refToPostProcessing);
postProcessingSync();
});

}

这是我的服务器输出:

I20150919-09:27:09.189(2)? execAsync()        
I20150919-09:27:09.210(2)? File names:
I20150919-09:27:09.213(2)? [ 'example1.json', 'example2.json' ]
I20150919-09:27:09.215(2)? postProcessing()
I20150919-09:27:09.217(2)? { name: 'Product',
I20150919-09:27:09.217(2)? properties:
I20150919-09:27:09.218(2)? { id:
I20150919-09:27:09.218(2)? { type: 'number',
I20150919-09:27:09.218(2)? description: 'Product identifier',
I20150919-09:27:09.218(2)? required: true },
I20150919-09:27:09.218(2)? name:
I20150919-09:27:09.218(2)? { description: 'Name of the product',
I20150919-09:27:09.219(2)? type: 'string',
I20150919-09:27:09.219(2)? required: true },
I20150919-09:27:09.219(2)? price: { type: 'number', minimum: 0, required: true },
I20150919-09:27:09.219(2)? tags: { type: 'array', items: [Object] } } }
I20150919-09:27:09.220(2)? { red: '#f00',
I20150919-09:27:09.221(2)? green: '#0f0',
I20150919-09:27:09.221(2)? blue: '#00f',
I20150919-09:27:09.221(2)? cyan: '#0ff',
I20150919-09:27:09.221(2)? magenta: '#f0f',
I20150919-09:27:09.221(2)? yellow: '#ff0',
I20150919-09:27:09.221(2)? black: '#000' }

假设您有以下结构:

your-meteor-project
├── .meteor
├── server
├── private
│ └── methods
│ └── example1.json
│ └── example2.json
└── …

关于javascript - 如何从 Meteor 中的文件夹中读取所有 JSON 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32657464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com