2017-02-23 35 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(); 
    }); 
} 

};

該網址看起來像http://localhost:1337/forms/formRecords?collection=quotes它返回json對象中的記錄。如果我嘗試使用不同的集合再次使用同樣的動作,像這樣http://localhost:1337/forms/formRecords?collection=users帆出現了錯誤類型錯誤:無法讀取的不確定我嘗試添加了orm.teardown()函數的性質「集合」,但它返回一個空查看(未定義)。任何想法如何重新初始化水線並加載新的集合?

+0

請求參數中的這些集合是否定義爲Models in Sails應用程序? – Sangharsh

+0

我給了用戶創建他們自己的表單的能力,這也在mongodb中創建了一個新的集合,因此沒有生成模型。我想要做的是檢索存儲在指定集合'req.param('collection')'這個代碼所做的記錄,但只要我要求一個新的集合它說** TypeError:不能讀取屬性的集合'未定義**。我認爲這是因爲吃水線已經初始化,我需要一種方法重新初始化吃水線併發送另一個收集請求。 – mblais29

+0

它使用['.native()']工作嗎?(http://sailsjs.com/documentation/reference/waterline-orm/models/native)?查詢? – Sangharsh

回答

1

我設法弄明白了。我喜歡打電話的動作,從而

localhost:1337/forms/formRecords?collection=collectionName

然後在我的formRecords動作看起來像這樣

'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(「收集」),它檢索所有記錄用於mongo數據庫中的任何集合。

相關問題