insert
成功,在应用崩溃后,它在数据库中完美无缺。
架构:
/**
* Section Schema
*/
var SectionSchema = new Schema({
name: {
type: String,
required: true
}
});
/**
* Report Schema
*/
var ReportSchema = new Schema({
created: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
trim: true,
required: 'Title cannot be blank'
},
sections: [SectionSchema],
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
mongoose.model('Report', ReportSchema);
控制器
/**
* Create a report
*/
exports.create = function (req, res) {
var report = new Report(req.body);
report.user = req.user;
report.sections = [];
// going over each name of sections and creating an empty object for each of them
req.body.sections.forEach(function (section) {
report.sections.push({
name: section
});
});
console.log(report);
report.save(function (err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(report);
}
});
};
错误:
/home/binny/Programming/project/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:779
catch(err) { process.nextTick(function() { throw err}); }
^
TypeError: self[i].emit is not a function
at EventEmitter.notify (/home/binny/Programming/project/node_modules/mongoose/lib/types/documentarray.js:214:15)
at emitTwo (events.js:92:20)
at EventEmitter.emit (events.js:172:7)
at model.Document.(anonymous function) [as emit] (/home/binny/Programming/project/node_modules/mongoose/lib/document.js:88:42)
at /home/binny/Programming/project/node_modules/mongoose/lib/model.js:229:11
at /home/binny/Programming/project/node_modules/mongoose/lib/model.js:139:7
at /home/binny/Programming/project/node_modules/mongoose/node_modules/mongodb/lib/collection.js:479:5
at /home/binny/Programming/project/node_modules/mongoose/node_modules/mongodb/lib/collection.js:633:5
at /home/binny/Programming/project/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:469:9
at resultHandler (/home/binny/Programming/project/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:416:5)
at /home/binny/Programming/project/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:778:13
at Callbacks.emit (/home/binny/Programming/project/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:95:3)
at null.messageHandler (/home/binny/Programming/project/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:249:23)
at Socket.<anonymous> (/home/binny/Programming/project/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:262:22)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at Socket.Readable.push (_stream_readable.js:110:10)
at TCP.onread (net.js:523:20)