node.js - Mongoose doesn't save data to the MongoDB -
below object literal trying save mongodb. defined within app.js file express server. object hardcoded within server, assumption new copy saved db every time run server, or @ least document saved once, , overridden or left unchanged upon detection new document identical 1 saved on last server run. astonishment, not no copies created within mongodb, document not saved @ all. however, 'news' collection has been created, verified mongo shell 'show collections'. also, not getting error in callback function. tried model.create(doc, fn) within express '/news' route, doesn't work (the doc should saved every time '/news' route called client, isn't). missing?
please, read annotations marked "<-" see other problems or unexpected behaviour encounter. grateful if address in answer.
var express = require('express') , routes = require('./routes') , user = require('./routes/user') , http = require('http') , path = require('path') , fs = require('fs'); // defining connection database: var mongoose = require('mongoose'). connect("mongodb://localhost:27017/my-test-db"), db = mongoose.connection; var schema = mongoose.schema; var objectid = schema.objectid; // setting 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 addedon , addedby fields before news article saved 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 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 mongodb: " + err); // <- gets executed when there's error console.error(news); // <- never gets logged, if there's no error. }); var app = express(); // 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 time
best regards
jared
it looks problem in 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 "next" callback must execute let mongoose know done , ready save document. since you're not calling it, explain why nothing saved, , no errors.
try calling next this:
newsschema.pre('save', function(next){ if( !this.addedon ) this.addedon = new date(); if( !this.addedby ) this.addedby = {first: "admin", last: "admin"}; next(); });
Comments
Post a Comment