【问题标题】:node.js with mongodb, after npm install mongodb, error: TypeError: url.indexOf is not a functionnode.js 与 mongodb,npm install mongodb 后,错误:TypeError: url.indexOf is not a function
【发布时间】:2016-01-07 15:52:13
【问题描述】:

我目前正在使用 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;

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:
    mongoClient.connect(function(err, mongoClient) {})
    

    您在这里使用了错误的功能。 mongoClient.connect('conntring',callback)

    mongoClient.open(function(err, mongoClient) {})
    

    【讨论】:

      猜你喜欢
      • 2015-12-09
      • 1970-01-01
      • 2017-11-19
      • 2022-10-22
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 2013-12-22
      • 2017-06-22
      相关资源
      最近更新 更多