2016-06-15 15 views
0

Mongoのコレクションごとに個別のファイルを含むフォルダがあります。私はディレクトリの内容を読んで、各コレクションを流星で動的に作成したいと考えています。同様の種類:NPMは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でのみ利用可能なサーバー側であるためであると理解しています。しかし、私はこれらのコレクションがクライアントでも利用できるようにする必要があります。メテオではこのようなことが可能ですか?

+0

をあなたは流星1.3またはそれ以前のバージョンを使用していますか? – thatgibbyguy

+0

メテオを使っているIm 1.3 – wazzaday

+0

corvidのコメンタントを参照してください。 1.3では、npmからインポートする新しい方法を紹介しています。 – thatgibbyguy

答えて

2

あなたが1.3を使用してサーバー上の流星を使用している場合、あなたはおそらくこのような何かしたい:

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]; 
} 
+0

ありがとうございましたcorvid、あなたがそのようにインポートすることができなかったことを理解していない! – wazzaday

関連する問題