2017-02-23 5 views
0

sails.jsでmongoコレクションをURLパラメータとして渡し、レコードを取得するアクションを作成しました。sails.jsとwaterlineを使用してmongodbのレコードを動的に取得します

'formRecords': function(req, res, next){ 

    var orm = new Waterline(); 

    var config = { 
     // Setup Adapters 
     // Creates named adapters that have been required 
     adapters: { 
      'default': 'mongo', 
      mongo: require('sails-mongo') 
     }, 
         // Build Connections Config 
     // Setup connections using the named adapter configs 
     connections: { 
      'default': { 
       adapter: 'mongo', 
       url: 'mongodb://localhost:27017/db' 
      } 
     } 
    }; 


    var record = Waterline.Collection.extend({ 
     identity: req.param('collection'), 
     connection: 'default' 
    }); 

    orm.loadCollection(record); 

    orm.initialize(config, function(err, models) { 
     var mongoCollection = models.collections[req.param('collection')]; 

     //May need to create a whole new page to re-direct to for form records so we can orm.teardown() like in the create action 
     mongoCollection.find() 
     .exec(function(err, result){ 
      console.log(result); 
      res.json(result); 
      /*res.view('forms/formRecords', { 
       data: result 
      });*/ 
     }); 
    //Must have orm.teardown() to close the connection then when adding a new collection I do not get the Connection is already registered error. 
     //orm.teardown(); 
    }); 
} 

}

URLは、http://localhost:1337/forms/formRecords?collection=quotesのように見え、jsonオブジェクトのレコードを返します。私はそうhttp://localhost:1337/forms/formRecords?collection=users帆エラー出TypeError例外のような別のコレクションにもう一度同じアクションを使用しようとすると:未定義のプロパティ「コレクション」を読み取ることができません私はorm.teardown()関数を追加しようとしたが、それは空白を返します。ビュー(未定義)。どのように新しいコレクションを読み込んで、水を再初期化するのか考えていますか?

+0

これらのコレクションは、Sailsアプリケーションのモデルとして定義されたリクエストパラメータですか? – Sangharsh

+0

私はユーザーに独自のフォームを作成し、mongodbにも新しいコレクションを作成できるようにしました。したがって、モデルは生成されません。私がしようとしているのは、このコードが行う指定されたコレクション 'req.param( 'collection')に格納されているレコードを取得することですが、新しいコレクションを要求するとすぐに** TypeError:プロパティ ' 'の定義されていない**。私は水線が既に初期化されていると思います。私は水線を再初期化し、別のコレクションの要求を送信する方法が必要です。 – mblais29

+0

['.native()'](http://sailsjs.com/documentation/reference/waterline-orm/models/native)を使って動作しますか?クエリ? – Sangharsh

答えて

1

私はそれを把握することができました。私はそう

'formRecords': function(req, res, cb){ 
    var findRecords = function(db, callback) { 
     // Get the collection records 
     var collection = db.collection(req.param('collection')); 
     // Find some records 
     collection.find({}).toArray(function(err, records) { 
     assert.equal(err, null); 

     //Returns the records found for the specified collection 
     res.json(records); 
     callback(records); 
     }); 
    }; 
    var MongoClient = require('mongodb').MongoClient 
     , assert = require('assert'); 

    // Connection URL 
    var url = 'mongodb://localhost:27017/databaseName'; 
    // Use connect method to connect to the Server 
    MongoClient.connect(url, function(err, db) { 
     assert.equal(null, err); 
     console.log("Connected correctly to server"); 
     findRecords(db, function() { 
      db.close(); 
     }); 
    }); 
} 

私は引数req.param(「コレクション」)に渡すと、それはすべてのレコードを取得するようなので、

localhost:1337/forms/formRecords?collection=collectionName

のようなアクションはその後、私のformRecordsにアクションが見え呼び出しますmongoデータベースの任意のコレクションに対して。

関連する問題