首页 文章

无法使用nodejs连接到本地mongodb服务器

提问于
浏览
1

当我尝试从项目目录连接到mongo db时,我得到了这个

/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / connect-mongo / lib / connect-mongo.js:133 throw err; ^ MongoError:无法在indexInformation(/ Users /)上的Collection.listIndexes(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / mongoose / node_modules / mongodb / lib / collection.js:1712:11)连接到服务器Tadeothompson / Documents / design work / stressful / site / node_modules / mongoose / node_modules / mongodb / lib / db.js:1531:25)在Db.indexInformation(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / mongoose / node_modules / mongodb / lib / db.js:1498:44)在ensureIndex(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / mongoose / node_modules / mongodb / lib / db.js:1003:8 )在EnsureIndex(/ Users / tadeothompson / Documents / design work)的Db.ensureIndex(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / mongoose / node_modules / mongodb / lib / db.js:982:44) /stressful/site/node_modules/mongoose/node_modules/mongodb/lib/collection.js:1772:13)在Collection.ensureIndex(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / mongoo) se / node_modules / mongodb / lib / collection.js:1760:44)在connectionReady(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / connect-mongo / lib / connect-mongo.js:141:27 )在initWithNativeDb的Db.collection(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / mongoose / node_modules / mongodb / lib / db.js:425:20)(/ Users / tadeothompson / Documents / design work) /stressful/site/node_modules/connect-mongo/lib/connect-mongo.js:207:20)在function.Module.runMain的process._tickCallback(node.js:355:11)处(module.js:503:11) )在node.js:814:3启动时(node.js:129:16)

设法使用简单的应用程序连接(下面的代码)

*

var MongoClient = require('mongodb').MongoClient;
    // Connect to the db
    MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
      if(!err) {
        console.log("We are connected");
      }
    });
the main node file of the app in question code is below:


var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var expressSession = require('express-session');
var mongoStore = require('connect-mongo')({session: expressSession});
var mongoose = require('mongoose');
require('./models/users_model.js');
var conn = mongoose.connect('mongodb://localhost:27017/stressfullproject');
var app = express();
app.engine('html', require('ejs')._express); 
app.set('views', './site' + '/views');        
app.set('view engine', 'html');
app.use(bodyParser());
app.use(cookieParser());
app.use(expressSession({
    secret: 'stress',
    cookie: {maxAge: 60*60*1000},
    store: new mongoStore({
        db: mongoose.connection.db,
        collection: 'sessions'
    })
}));
require('./routes/routes')(app);
app.listen(80);

我定义的架构

*var mongoose = require('mongoose'),
    Schema = mongoose.Schema;
var UserSchema = new Schema({
    username: { type: String, unique: true },
    email: String,
    hashed_password: String
})
mongoose.model('User', UserSchema)*

因为我可以与其他应用程序连接,我认为它是我的一个模块的问题?香港专业教育学院搜遍了

提前致谢

3 回答

  • 1

    我最好的猜测是你使用两个模块,即MongoClient和mongoose都试图连接到端口27017.现在在这场比赛中只有一个会赢,并将锁定该端口 . 如果您尝试绑定到该端口,它将给您一个错误,类似于您上面提到的错误 . 我的建议,不要使用MongoClient . 只使用猫鼬 . mongoose有很多帮助,youtube上的许多视频教程都使用它 . 如果这没有帮助让我知道 .

    让我们为你准备好一些代码 . 我现在不使用MongoClient,但是当我以前编写这段代码时,看到它的工作原理 . 如果没有,请粘贴堆栈跟踪 .

    var MongoClient=require('mongodb').MongoClient,
    
    server=require('mongodb').Server;
    
    var mongoclient=new MongoClient(new server('localhost',27017));
    
    mongoclient.connect('mongodb://localhost:27017/course',function(err,db)
    {
        if(err) throw err;
        //var db=mongoclient.db('course');
        var query={'grade':100};
    
        db.collection('grades').findOne(query,function(err,doc)
            {
                if(err) throw err;
                console.dir(doc);
                db.close();
            });
    });
    
  • 1

    我在另一个堆栈溢出后发现了答案here

    问题是在mongoose Build 连接之前,会话(或mongoose之外的其他东西)试图连接到数据库 .

  • 0

    两个问题中的任何一个 - 要么是调用未定义的模式(mongoose),要么你有一个需要next但next的函数是未定义的 . 我多次遇到这个问题,并且记录了mongoose缺少错误处理 . 您需要在app.js文件的早期定义一些错误处理 .

相关问题