我有一个包含不同Mongo集合的单个文件的文件夹。我想阅读目录的内容,并在meteor中动态创建每个集合。有点像:
let collections = {};
let fs = Npm.require('fs');
let files = fs.readDirSync('path-to-my-folder');
files.forEach(fileName, () => {
let schema = Npm.require('path-to-my-folder/'+fileName);
let collection = new Mongo.Collection(fileName);
collections[fileName] = collection; //store collection
// Create method, and publications for each collection
});
// export function to get any collection by name
export default function(name){
return collections[name];
}
这里的问题是,当我加载网站时,我收到错误 Npm未定义
据我所知,这是因为Npm只是服务器端。但我也需要在客户端上提供这些集合。 Meteor有可能做到这一点吗?
答案 0 :(得分:2)
如果你在1.3服务器上使用Meteor,你可能想要这样的东西:
import { Mongo } from 'meteor/mongo';
import { readDirSync } from 'fs';
export let collections = {};
readDirSync('some-dir/').forEach(file => {
const schema = require(`./${file}`);
const collection = new Mongo.Collection(file);
collections[file] = collection;
})
export default function getCollection(name) {
return collections[name];
}