0

I'm working on a new app and trying to get mongo set up locally for storage. My api endpoints are getting hit but as soon as I try to actually call a db operation - find or save - it doesn't respond.

var express = require('express');
var router = express.Router();

var bodyParser = require('body-parser');

var mongoose = require('mongoose');
var Person = require('./data/person');
var dbConfig = require('./config');
//database: 'mongodb://localhost:27017/persondb'
var db = mongoose.createConnection(dbConfig.database);
db.on('error', function() {
    console.info('Error: Could not connect to MongoDB. Did you forget      to run `mongod`?');
});

if (~process.argv.indexOf('mode_dev')) {
    global.mode_dev = true;
    console.log('Server started in dev mode.');
}


// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use('/api', router);

router.route('/persons')
    .post(function(req, res) {
        debugger;
        var person = new Person();      // create a new instance of the Person model
        person.name = req.body.name;

        person.save(function(err) {
            debugger;
            if (err)
                res.send(err);

            res.json({ message: 'Person created!' });
        });
    })
    .get(function(req, res) {
         Person.find(function(err, persons) {
            debugger;
            if (err)
                res.send(err);

            res.json(persons);
        });
    });

Here's the schema:

data/item.js

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

var personSchema = new Schema({
    name: String,
});

module.exports = mongoose.model('Person', personSchema);

I am running mongod before getting my webserver started. As soon as I get to the .save or .find calls, no error is thrown but the callbacks are never hit.

alsoALion
  • 449
  • 1
  • 5
  • 17
  • What happens when you do console.log(req.body) in your post? do you get the name attribute's value? – jack blank Jan 24 '16 at 04:54
  • @jackblank yes, I do. +++ – alsoALion Jan 24 '16 at 05:05
  • Refer to http://stackoverflow.com/questions/13162979/confusion-between-mongoose-connection-and-mongoose-createconnection, http://stackoverflow.com/questions/22786374/queries-hang-when-using-mongoose-createconnection-vs-mongoose-connect – zangw Jan 24 '16 at 05:43

1 Answers1

2

I would connect like this:

mongoose.connect("mongodb://localhost/persondb");

var db = mongoose.connection;

maybe this will help it explains problems using mongoose.createConnection(

if you use createConnection, you need to always reference that connection variable if you include a registered model, otherwise if you use mongoose to load the model, it will never actually talk to your database.

The other solution is to simply use mongoose.connect instead of mongoose.createConnection. Then you can just use mongoose.model(‘MyModel’) to load the requested model without needing to carry around that db connection variable.

jack blank
  • 5,073
  • 7
  • 41
  • 73
  • That did it... I had actually read that thread but apparently not made the right updates to use .connect. Thanks!! – alsoALion Jan 24 '16 at 17:37