我目前正在使用MongoDB,Express和NodeJS尝试一个简单的Hello World应用程序。
教程为How To Write A Simple Node.js/MongoDB Web Service for an iOS App
当我运行没有npm install mongodb的index.js时,一切都很好,这里的代码如下所示。
在修改package.json" mongodb":" 1.3.23"和
npm update
然后再次运行index.js,错误在这里:
user$ node index.js
Failed to load c++ bson extension, using pure JS version
/Users/liyan/Documents/OneDrive/MScCS/fos/testdb/node_modules/mongodb/lib/mongodb/connection/url_parser.js:14
if(url.indexOf("mongodb://") != 0)
^
TypeError: url.indexOf is not a function
at exports.parse (/Users/liyan/Documents/OneDrive/MScCS/fos/testdb/node_modules/mongodb/lib/mongodb/connection/url_parser.js:14:10)
at Function.MongoClient.connect (/Users/liyan/Documents/OneDrive/MScCS/fos/testdb/node_modules/mongodb/lib/mongodb/mongo_client.js:167:16)
at MongoClient.connect (/Users/liyan/Documents/OneDrive/MScCS/fos/testdb/node_modules/mongodb/lib/mongodb/mongo_client.js:80:15)
at Object.<anonymous> (/Users/liyan/Documents/OneDrive/MScCS/fos/testdb/index.js:19:13)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:430:10)
at startup (node.js:141:18)
index.js
var http = require('http'),
express = require('express');
path = require('path');
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
CollectionDriver = require('./collectionDriver').CollectionDriver;
bson = require('bson');
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
var mongoHost = 'localHost'; //A
var mongoPort = 27017;
var collectionDriver;
var mongoClient = new MongoClient(new Server(mongoHost, mongoPort)); //B
mongoClient.connect(function(err, mongoClient) { //C
if (!mongoClient) {
console.error("Error! Exiting... Must start MongoDB first");
process.exit(1); //D
}
var db = mongoClient.db("MyDatabase"); //E
collectionDriver = new CollectionDriver(db); //F
});
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function (req, res) {
res.send('<html><body><h1>First page res.send </h1></body></html>');
});
app.get('/:collection', function(req, res) { //A
var params = req.params; //B
collectionDriver.findAll(req.params.collection, function(error, objs) { //C
if (error) { res.send(400, error); } //D
else {
if (req.accepts('html')) { //E
res.render('data',{objects: objs, collection: req.params.collection}); //F
} else {
res.set('Content-Type','application/json'); //G
res.send(200, objs); //H
}
}
});
});
app.get('/:collection/:entity', function(req, res) { //I
var params = req.params;
var entity = params.entity;
var collection = params.collection;
if (entity) {
collectionDriver.get(collection, entity, function(error, objs) { //J
if (error) { res.send(400, error); }
else { res.send(200, objs); } //K
});
} else {
res.send(400, {error: 'bad url', url: req.url});
}
});
app.use(function (req,res) { //1
res.render('404', {url:req.url}); //2
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
collectionDriver.js
var ObjectID = require('mongodb').ObjectID;
CollectionDriver = function(db) {
this.db = db;
};
CollectionDriver.prototype.getCollection = function(collectionName, callback) {
this.db.collection(collectionName, function(error, the_collection) {
if( error ) callback(error);
else callback(null, the_collection);
});
};
//find all objects for a collection
CollectionDriver.prototype.findAll = function(collectionName, callback) {
this.getCollection(collectionName, function(error, the_collection) { //A
if( error ) callback(error)
else {
the_collection.find().toArray(function(error, results) { //B
if( error ) callback(error)
else callback(null, results)
});
}
});
};
//find a specific object
CollectionDriver.prototype.get = function(collectionName, id, callback) { //A
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error)
else {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$"); //B
if (!checkForHexRegExp.test(id)) callback({error: "invalid id"});
else the_collection.findOne({'_id':ObjectID(id)}, function(error,doc) { //C
if (error) callback(error)
else callback(null, doc);
});
}
});
}
exports.CollectionDriver = CollectionDriver;
答案 0 :(得分:1)
mongoClient.connect(function(err, mongoClient) {})
你在这里使用错误的功能。 mongoClient.connect(&#39; conntring&#39;,回调)
mongoClient.open(function(err, mongoClient) {})