我喜欢在另一个文档中嵌入文档。 这里我的第一个猫鼬文件包括第二个“格栅”: chp_bat.js
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId
, Grille = require('../models/grille');
function validatePresenceOf(value) {
return value && value.length;
}
var Chp_bat = new Schema({
name : { type: String, validate: [validatePresenceOf, 'a chp_bat is required'] },
avatar : { type: String },
description : { type: String },
grille : [Grille],
created_at : Date,
updated_at : Date
});
var Chp_bat = mongoose.model('Chp_bat', Chp_bat);
module.exports = mongoose.model('Chp_bat', Chp_bat);
我的第二份文件:grille.js
mongoose = require('mongoose');
Schema = mongoose.Schema;
ObjectId = Schema.ObjectId;
function validatePresenceOf(value) {
return value && value.length;
}
var Grille = new Schema({
name : { type: String, validate: [validatePresenceOf, 'a grille is required'] },
avatar : { type: String },
description : { type: String },
size : { x: Number, y: Number },
created_at : Date,
updated_at : Date
});
var Grille = mongoose.model('Grille', Grille);
module.exports = mongoose.model('Grille', Grille);
我的路线:
var User = require('../../models/user')
, Chp_bat = require('../../models/chp_bat')
, Grille = require('../../models/grille');
module.exports = function (app){
/*
* GET New chp_bat.
*/
app.get('/chp_bat_new', ensureAuthenticated, function (req, res){
Grille.find({}, function (err, docs) {
res.render('chp_bat/new', {
docs: docs,
user : req.user,
title : ' Que voulez vous créer Maitre Du Jeu'
});
});
});
/*
* POST New chp_bat.
*/
app.post('/maitre-chp_bat-new', ensureAuthenticated, function (req, res){
var chp_bat = new Chp_bat();
chp_bat.name = req.body.name;
chp_bat.avatar = req.body.avatar;
chp_bat.description = req.body.description;
chp_bat.grille.push(req.body.grille);
chp_bat.created_at = new Date();
chp_bat.save(function (err) {
if (!err) {
res.redirect('/maitre-chp_bat-new');
console.log(req.body);
}
else {
res.redirect('/maitre-chp_bat-new');
}
});
});
};
我收到此错误:对象{size:{}}没有方法'cast' 我觉得自己很好,制作像mongoose docs一样的东西,我不明白我做得不好。
====
所以,就像你发布问题时一样,答案来了。 我是为其他人写的:
当您编写嵌入的文档时,请包含_id,如下所示:
grille: [Grille._id],
太容易了。
答案 0 :(得分:0)
将你的文档链接在一起考虑做这样的事情:
app.js
var express = require('express'),
app = express(),
require('./routes')(app);
}
console.log('Listening on port 8888');
路由/ index.js
module.exports = function(app){
var Users = require('../controllers/users')(app);
app.get('/logout',Users.profile);
}
控制器/ users.js
module.exports = function(app){
var User = require('../models/users')(app),
return {
profile: function (req, res) {
User.findOne(
{'email' : 'your@email.com'},
'_id name email modifiedOn',
function(err, user) {
if (!err) {
//do something with user
} else {
res.send({"error": "some error happened"});
}
}
);
},
login: function(req,res){
//login to login here. you know like req.body.email and req.body.password etc.
}
}
}
模型/ users.js
module.exports = function(app){
var db = require('../lib/db')(app);
var userSchema = new db.Schema({
first_name: {
type: String,
required: true
},
last_name: {
type: String,
required: true
},
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
createdOn: {
type: Date,
default: Date.now
},
modifiedOn: {
type: Date,
default: Date.now
},
lastLogin: {
type: Date,
default: Date.now
}
});
db.model( 'User', userSchema);
return db.model('User');
}
LIB / db.js
module.exports = function(app){
var mongoose = require('mongoose');
var uri = 'mongodb://'+app.get('db_host')+':'+app.get('db_port')+'/'+app.get('db_database');
var options = {}
mongoose.connect(uri,options);
mongoose.connection.on('connected', console.log.bind(console, 'Mongoose connected to ' + uri));
mongoose.connection.on('error',console.error.bind(console, 'connection error:'));
mongoose.connection.on('disconnected', console.log.bind('Mongoose disconnected'));
process.on('SIGINT', function() {
mongoose.connection.close(function () {
console.log('Mongoose disconnected through app termination');
process.exit(0);
});
});
return mongoose;
}
请注意,在此示例中,我使用db_host,port和database的配置项。就个人而言,这个设置对我很有用。当您的应用程序变得更大时,它会派上用场。
答案 1 :(得分:0)
由于
,您收到此错误chp_bat.grille.push(req.body.grille);