我對mongodb和node.js的世界很陌生。 我有一個項目,我把mongodb代碼放在一個路徑中,我需要在我的server.js中。Node.js,mongodb返回函數的字符串表示形式,而不是輸出
現在在該模塊中我有一個方法將返回一個集合中的所有條目(它的工作原理)。
我想從server.js文件調用該函數,但我通常以響應打印出函數,而不是執行它並返回輸出!
例子:
var http = require('http'),
location = require('./routes/locations');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write(location.findAll() + '');
response.end();
}).listen(8080);
現在,當我直接把我的UI到8080,我想location.findall的輸出,而不是我得到一個不確定的消息,並在節點以下異常:
TypeError: Cannot call method 'send' of undefined
我知道這可能是一個新手問題,我來自java,.NET和iOS世界。抱歉!!
更新:澄清更多的,這裏是我的路線/ locations.js
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('locationsdb', server);
db.open(function(err, db) {
// initlization code
});
exports.findAll = function(req, res) {
db.collection('locations', function(err, collection) {
collection.find().toArray(function(err, items) {
res.send(items);
});
});
};
感謝,雖然這有點兒幫助從一個錯誤打動我到另一個錯誤,我想我有我的假設,揭露更多。 –