Question

Trying to figure this out. So confused/annoyed

[Server 1] is my app server. In it lives:

app.js

var mongoose = require('mongoose'),
    express = require("express"),
    app = express();

mongoose.connect('mongodb://mongo:mymongopass@[SERVER 2 IP]'); 

app.configure(function () {
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
});


app.get('/', function (req, res) {
    console.log('hi');
    res.send('hello world');
});

// auctions api
var auctions = require('./auctions.js');
app.get('/auctions', auctions.list);
app.get('/auctions/create', auctions.create);

app.listen(3000);

auctions.js

// load up the model
var Auction = require('./models/auction.js');


exports.create = function (req, res) {
    // replace data assigments with something like
    // {comments: req.body.comments}
    new Auction({
        iso_currency_symbol: 'GBP',
        comments: 'second auction'
    }, function(err, resp) {
console.log('wat');}).save();

    res.send('hello, this is response text');
}


exports.list = function (req, res) {
    Auction.find(function (err, auctions) {
        res.send(auctions);
    });
}

./models/auction.js

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId;

var auction_schema = new Schema({
    iso_currency_symbol: String,
    created_date: { type: Date, default: Date.now },
    started_date: { type: Date },
    owner_id: ObjectId,
    comments: String
});

module.exports = mongoose.model('Auction', auction_schema);

[Server 2] is my database server. On it lives mongodb.

From [Server 1], I can connect to [Server 2] with: mongo [Server 2]. It will connect. I can also insert records in the collections.

But in my app (above), it won't connect. [Server 2] doesn't have any crazy firewalls (I didn't mess with ip tables or anything or ufw yet).

Trying to figure out why Mongoose isn't connecting. It's not even throwing any errors. Not sure what the hell is going on (if it's a problem with my servers or Mongoose itself).

They are both Ubuntu servers fwiw. Any help would be amazing. Thanks.

Was it helpful?

Solution

It doesn't appear that a database name has been specified:

mongoose.connect('mongodb://mongo:mymongopass@[SERVER 2 IP]/databaseName');
mongoose.connect('mongodb://mongo:mymongopass@[SERVER 2 IP]', 'databaseName');

Normally, this will default to test. But, the user/pass may not have access to use that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top