oderfla
oderfla

Reputation: 1797

Mongodb not saving through express.js

OS is Os X. I installed mongodb through brew.

I open a terminal and enter

mongod

A few rows are then printed in the terminal. DB seems definitely up and running. I see for example:

db version v3.4.3
waiting for connections on port 27017

I then open another terminal and enter

mongo

I see then:

MongoDB shell version v3.4.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.3           

I then type in this terminal

show dbs

And I see:

admin  0.000GB
api    0.000GB
local  0.000GB  

So I enter

use api
db.movie.find();

The terminal prints:

{ "_id" : ObjectId("58f34b96d7eac221145a5d4e"), "name" : "tutorials point" }
{ "_id" : ObjectId("58f367e198554c65c4a34c9f"), "name" : "lalaland" }       

I enter:

db.movie.insert({ name: "forreste gump"});

and then the find() command returns:

{ "_id" : ObjectId("58f34b96d7eac221145a5d4e"), "name" : "tutorials point" }
{ "_id" : ObjectId("58f367e198554c65c4a34c9f"), "name" : "lalaland" }
{ "_id" : ObjectId("58f369259a667b015582e4b8"), "name" : "forreste gump" }

Now, I create a new node app with express.js. This is the code:

var express    = require('express');
var app        = express();
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var connection = mongoose.createConnection('mongodb://localhost/api');

connection.on('open', function() {
    console.log('opened');
});

var movieSchema = mongoose.Schema({
    name: String
});

var Movie = mongoose.model('Movie', movieSchema);
var pulp = new Movie({ name: 'pulp fiction' });
pulp.save(function (err, fluffy) {
    if (err){
        console.error("Error:");
        console.error(err);
    }
    console.error('inside save');
});
console.log('pulp.name');
console.log(pulp.name);

In package.json I entered

"mongoose": "~4.9.4",

and then run npm install (I can see that version has been installed)

So, when I start the app with

node app.js

In the terminal I see

pulp.name
pulp fiction
opened      

This means that the line "pulp.save" is never excecuted. Or, probably, is stopped by some error that doesnt show up. Im getting crazy on this. I really don't know what the problem is. mongod is up and running, I connect to the right database in mongoose. But still cannot save a document and, most important, I don't see any trace of errors. Which apparently must occur somewhere. How can I debug deeply the mongoose operations?

PS: When I go to terminal where I run mongo client, and I type:

db.movie.find()

I only see

{ "_id" : ObjectId("58f34b96d7eac221145a5d4e"), "name" : "tutorials point" }
{ "_id" : ObjectId("58f367e198554c65c4a34c9f"), "name" : "lalaland" }
{ "_id" : ObjectId("58f369259a667b015582e4b8"), "name" : "forreste gump" }

So "Pulp fiction" was never added.

Upvotes: 0

Views: 330

Answers (1)

Ties
Ties

Reputation: 805

The difference is mongoose.connect() vs mongoose.createConnection() (Also see this question)

Basically works as follows: When usingmongoose.createConnection() you have to do:

var db = mongoose.createConnection(...)
// And use the db variable
var Model = db.model(...); // etc...

Instead of using mongoose.model(...).

This is because by using mongoose.createConnection() you're not creating a connection on the mongoose object, only inside the scope of the db object.

Upvotes: 1

Related Questions