猫鼬从不连接到mongodb [英] Mongoose never connects to mongodb

查看:78
本文介绍了猫鼬从不连接到mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Amazon EC2 Linux服务器上使用Mongoose连接到MongoDB.

I'm trying to connect to MongoDB using Mongoose on an Amazon EC2 Linux server.

这是我的代码:

var mongoose = require('mongoose');
console.log("Attempting antyhing to do with mongoose"); //shown

var db = mongoose.connection;
db.on('error',console.error.bind(console,'db connection error:')); //not shown
db.once('open',function(){
    console.log("Successful connection to db!"); //not shown
});

mongoose.connect('mongodb://localhost:27017/local',function(err){
    console.log("some kinda connection made"); //not shown
    if(err)
    {
        console.log("err: "+err);
    }
});

令人沮丧的是,我没有从猫鼬那里得到任何错误,但是似乎什么也没有出现.

Frustratingly, I'm not getting any errors from mongoose whatsoever, but nothing seems to show up.

关于Mongoose和Mongo没有回调的问题似乎有很多.

There seem to be a lot of questions about no callback with mongoose and mongo.

以下是我看过的几对,我认为这对我来说不是问题:

Here's a couple that I've looked at that I don't think are the problem for me:

  • Listen for the callback quickly: Mongoose Connection I moved my db.on('open'... call to before my connect call in case of a race condition.
  • Is Mongo running? Mongoose connect method fails on simple Node Server. Express, Mongoose, Path Yes, and on port 27017

我也参考此教程:我担心的一件事是我将代码拆分成多个文件.因此,此猫鼬连接代码是从app/models/host.js(或教程中的bear.js)文件中调用的.让我知道发布其他文件是否有帮助.

One thing I am doing that I'm worried about is I've split my code up into multiple files. So this mongoose connection code is being called from a app/models/host.js (or bear.js in tutorial) file. Let me know if posting the other files would be helpful.

推荐答案

我也遇到了同样的问题.

I also faced the same issue.

检查您使用的Mongoose版本是否支持MongoDb服务器版本

Check that the Mongoose version you are using supports the MongoDb server version

在此链接上检查兼容性: http://mongoosejs.com/docs/compatibility.html

Check compatibility on this link: http://mongoosejs.com/docs/compatibility.html

相应地在package.json文件中更改Mongoose的版本.

Change the version of Mongoose in package.json file accordingly.

希望这会有所帮助!

这篇关于猫鼬从不连接到mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆