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()関数を追加しようとしたが、それは空白を返します。ビュー(未定義)。どのように新しいコレクションを読み込んで、水を再初期化するのか考えていますか?
これらのコレクションは、Sailsアプリケーションのモデルとして定義されたリクエストパラメータですか? – Sangharsh
私はユーザーに独自のフォームを作成し、mongodbにも新しいコレクションを作成できるようにしました。したがって、モデルは生成されません。私がしようとしているのは、このコードが行う指定されたコレクション 'req.param( 'collection')に格納されているレコードを取得することですが、新しいコレクションを要求するとすぐに** TypeError:プロパティ ' 'の定義されていない**。私は水線が既に初期化されていると思います。私は水線を再初期化し、別のコレクションの要求を送信する方法が必要です。 – mblais29
['.native()'](http://sailsjs.com/documentation/reference/waterline-orm/models/native)を使って動作しますか?クエリ? – Sangharsh