W
W
whiteleaf2015-08-27 09:02:32
MongoDB
whiteleaf, 2015-08-27 09:02:32

How to properly connect to MongoDB on C9?

Hello,
It seems that I am doing everything according to the instructions :), but I get an error when starting the application on Node.js
Where did I make a mistake?
Server.js

var express = require('express');
var app = express();
var path = require('path');

var port = process.env.PORT;

var mongoose = require('mongoose');
var configDB = require('./server/config/database.js');

mongoose.connect(configDB.url);

app.set('view engine', 'ejs');
app.set('views', path.resolve(__dirname, 'client', 'views'));

app.use(express.static(path.resolve(__dirname,'client')));

app.get('/', function(req,res){
  res.render('index.ejs');
});

app.listen(port, function(){
  console.log('Server is running... PORT ' + port);
});

Database.js
module.exports = {
    'url': 'mongodb://' + process.env.IP
}

Customer.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var customerSchema = mongoose.Schema({
    firstname: String,
    lastname: String,
    phone: String,
    address: {
        street: String,
        city: String,
        state: String,
        zip: String
    }
});

module.exports = mongoose.model('Customer',customerSchema);

Err
/home/ubuntu/workspace/node_modules/mongoose/node_modules/mongodb/lib/server.js:228
        process.nextTick(function() { throw err; })
                                            ^
Error: connect ECONNREFUSED
    at errnoException (net.js:905:11)
    at Object.afterConnect [as oncomplete] (net.js:896:19)

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill, 2015-08-27
@kirill89

This error can mean:
- the server is not running
- user authorization is needed
- the database does not exist (here I'm not sure, like the monga should create itself)
- the port is different from default
Check these moments, most likely your problem will be solved.
UPD:
By the way, your base is not specified at all. Try for starters like in the monguz documentation mongoose.connect('mongodb://localhost/myapp');.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question