0
我正在玩node.js並基於此wonderful tutorial我創建了兩個提供程序(SchemaProvider和EntityProvider)。node.js:導入模塊的範圍
他們看都像是:
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;
EntityProvider = function(host, port) {
this.db = new Db('timerange', new Server(host, port, {auto_reconnect: true}, {}));
this.db.open(function() {
console.log("Schema Provider has connected and may be used as of now.");
});
};
EntityProvider.prototype.getCollection = function(callback) {
this.db.collection('entity', function(error, collection) {
if (error) {
callback(error)
} else {
callback(null, collection);
}
});
};
EntityProvider.prototype.findById = function(id /* The id to be found */, callback) {
this.getCollection(function(error, collection) {
if (error) {
callback(error);
} else {
collection.findOne({_id: id}, function(error, result) {
if (error) {
callback (error);
} else {
callback(null, result);
}
});
}
});
};
在app.js我要求其中兩個供應商的定義( '供應商')。
然後我做的:
schemaProvider = new SchemaProvider('192.168.0.50', 27017);
entityProvider = new EntityProvider('192.168.0.50', 27017);
現在,我創建了一個名爲dao
模塊(我來自一個Java /彈簧觀點:-))。由於我沒有使用「var」,因此這兩個變量和提供者都可以在我的DAO中訪問。如果我使用「var」,那麼提供者將無法訪問。
我的問題是:
如果我想使用在整個應用程序只有一個供應商的情況下,我會怎麼做呢?
在此先感謝!
我會測試它儘快:-) – Jan
好吧,現在我有一個單身,但另一個問題。我調用this.getCollection,然後執行一個collection.find({})。toArray(function(err,arr){console.log(arr)});但沒有任何反應:( – Jan