下面是我想要保存到MongoDB的对象文字。它在app.js文件中定义,该文件是Express服务器。由于对象在服务器中是硬编码的,我的假设是每次运行服务器时都会将新副本保存到数据库中,或者至少文档将被保存一次,并且在检测到文档时将被覆盖或保持不变。新文档与上次运行的服务器上保存的文档相同。令我惊讶的是,不仅没有在MongoDB中创建副本,而且根本没有保存文档。然而,已经创建了'news'集合,正如mongo shell'show collections'所证实的那样。此外,我没有在回调函数中收到任何错误。我也在我的Express'/ news'路线中尝试过Model.create(doc,fn),但这也行不通(每次客户调用'/ news'路径时都应该保存文档,但是是不是)。我错过了什么?
请阅读标有“< - ”的注释,看看我遇到的其他问题或意外行为。如果你能在答案中解决这些问题,我将非常感激。
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'));
});
感谢您的时间 最好的问候 贾里德
答案 0 :(得分:25)
看起来问题出在你的新闻架构的保存中间件中。
newsSchema.pre('save', function(next){
if( !this.addedOn ) this.addedOn = new Date();
if( !this.addedBy ) this.addedBy = {first: "admin", last: "admin"};
});
您的函数会收到一个“下一个”回调,您必须执行该回调才能让mongoose知道您已完成并准备好保存文档。既然你没有调用它,它可以解释为什么你没有得到任何保存,也没有错误。
尝试像下一样调用下一个:
newsSchema.pre('save', function(next){
if( !this.addedOn ) this.addedOn = new Date();
if( !this.addedBy ) this.addedBy = {first: "admin", last: "admin"};
next();
});
答案 1 :(得分:5)
我尝试在本地重现这个问题时发现了一些问题。
你没有在newsSchema.pre('save')
中调用next()应该是
newsSchema.pre('save', function(next){
if( !this.addedOn ) this.addedOn = new Date();
if( !this.addedBy ) this.addedBy = adminUser;
next();
});
您还需要确保在执行任何此类操作之前已连接到数据库,不确定您是否存在,因为我没有看到该部分代码。
答案 2 :(得分:0)
我的代码不同,但我的结果显然是相同的:显然,尽管有.save调用,但我并没有保存到Mongo。但是,保存实际上正在发生,我只是没有意识到一些Mongoose参数的意思,并且需要一些自由来形成你的集合名称。
更具体地说,当你使用:
mongoose.model('MyModelName', invitationSchema);
要形成您的集合名称,您的模型名称将转换为小写,并附加“s”(如果不存在)。另请参阅http://samwize.com/2014/03/07/what-mongoose-never-explain-to-you-on-case-sentivity/
如果需要,您可以在创建架构时使用集合名称参数在某种程度上绕过这些集合命名约定。见http://mongoosejs.com/docs/guide.html#collection
这是我的:
const modelName = "SharingInvitation";
const collectionName = modelName + "s";
const numberOfHoursBeforeExpiry = 24;
var expiryDate = new Date ();
expiryDate.setHours(expiryDate.getHours() + numberOfHoursBeforeExpiry);
var invitationSchema = new Schema({
// _id: (ObjectId), // Uniquely identifies the invitation (autocreated by Mongo)
// gives time/day that the invitation will expire
expiry: { type: Date, default: expiryDate },
// The user is being invited to share the following:
owningUser: ObjectId, // The _id of a PSUserCredentials object.
capabilities: [String] // capability names
}, { collection: collectionName });