Below is an object literal I am trying to save to MongoDB. It is defined within the app.js file which is an Express server. As the object is hardcoded within the server, my assumption was that a new copy would be saved to the DB every time I run the server, or at the very least the document would be saved once, and overridden or left unchanged upon detection that the new document is identical to the one saved on the last server run. To my astonishment, not only no copies are created within the MongoDB, but the document is not saved at all. However, the 'news' collection has been created, as verified with mongo shell 'show collections'. Also, I am not getting any error in the callback function. I also tried the Model.create(doc, fn) within my Express '/news' route, but this also doesn't work (the doc should be saved every time the '/news' route is called by the client, but it isn't). What am I missing?
Please, read my annotations marked with "<-" to see what other problems or unexpected behaviour I encounter. I will be very grateful if you could address those as well in your answer.
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, fs = require('fs');
// Defining connection to the database:
var mongoose = require('mongoose').
connect("mongodb://localhost:27017/my-test-db"),
db = mongoose.connection;
var Schema = mongoose.Schema;
var ObjectID = Schema.ObjectId;
// Setting up the debug flag:
mongoose.set('debug, true');
// Logging connection:
db
.on('error', console.error.bind(console, 'DB connection error.'))
.once('open', console.log.bind(console, 'DB Connection established.'));
// Defining MongoDB schemas:
var usr = new Schema({
first: String,
last: String
});
var newsSchema = new Schema({
headline: String,
bd: String,
imgURI: String,
imgThumbURI: String,
imgCaption: String,
addedOn: Date,
addedBy: {
type: ObjectID,
ref: 'usr'
}
// On user action 'save' populate the addedOn and addedBy fields before the news article is actually saved to the DB:
newsSchema.pre('save', function(next){
if( !this.addedOn ) this.addedOn = new Date();
if( !this.addedBy ) this.addedBy = {first: "admin", last: "admin"};
});
// Indexing important fields:
usr.index({last: 1});
newsSchema.index({headline: 1});
//Adding the News model:
var News = mongoose.model('news', newsSchema);
var nws1 = new News({
headline: "Test news Headline",
bd: "Test news body. Test news body. Test news body. Test news body. Test news body. ",
imgURI: encodeURI("images/news/img.jpg"),
imgThumbURI: encodeURI("images/news/thumbs/img.jpg"),
imgCaption: "Test news image caption.",
addedOn: new Date(),
addedBy: {first: "Admin", last: "Admin"}
});
nws1.save(function(err, news){
if(err) return console.error("Error while saving data to MongoDB: " + err); // <- this gets executed when there's an error
console.error(news); // <- this never gets logged, even if there's no error.
});
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.resolve(__dirname + '/public'));
app.set('view engine', 'html')
.engine('html', function(path, options, fn){
if('finction' == typeof options){
fn = options, options = {};
}
fs.readFile(path, 'utf8', fn);
});
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session());
app.use(express.static(path.join(__dirname, 'public')));
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Thank you for your time
Best regards
Jared
To save the data into the database, we need to create a new instance of our model that we created early. We will pass into this instance the user's input. Once we have it then we just need to enter the command "save". Mongoose will return a promise on a save to the database.
save() is a method on a Mongoose document. The save() method is asynchronous, so it returns a promise that you can await on. When you create an instance of a Mongoose model using new, calling save() makes Mongoose insert a new document.
Connecting to MongoDBMongoose requires a connection to a MongoDB database. You can require() and connect to a locally hosted database with mongoose. connect() as shown below (for the tutorial we'll instead connect to an internet-hosted database). You can get the default Connection object with mongoose.
You can connect to MongoDB with the mongoose.connect() method. mongoose.connect('mongodb://localhost:27017/myapp'); This is the minimum needed to connect the myapp database running locally on the default port (27017). If connecting fails on your machine, try using 127.0.0.1 instead of localhost .
It looks like the problem is in your news schema's save middleware.
newsSchema.pre('save', function(next){
if( !this.addedOn ) this.addedOn = new Date();
if( !this.addedBy ) this.addedBy = {first: "admin", last: "admin"};
});
Your function receives a "next" callback which you must execute to let mongoose know that you are done and ready to save the document. Since you're not calling it, it could explain why you get nothing saved, and also no errors.
Try just calling next like this:
newsSchema.pre('save', function(next){
if( !this.addedOn ) this.addedOn = new Date();
if( !this.addedBy ) this.addedBy = {first: "admin", last: "admin"};
next();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With