我正在编写一个vivarium软件,并尝试使用node.js和mongodb来实现,但是我遇到了猫鼬的一些问题。
|| route.js
var animalspecieSchema = new Schema({
name: {type: String, required: true},
}, {collection: 'animal-specie'});
var animalspecie = mongoose.model('animalspecie', animalspecieSchema);
var cageSchema= new Schema({
name: {
type: String, required: true
},
animalspecie: {
type: Schema.Types.ObjectId,
ref: 'animalspecie'
}
}, {collection: 'cage'});
var cage= mongoose.model('cage', cageSchema);
router.get('/cage',
function(req,res,next){
cage.find().populate('animalspecie').then((cages) => {
res.render('cage', {cages: cages})
})
|| cage.hbs
<h2 class="mt-4">Cages</h2>
{{#each cages}}
<div class="col-md-12 mt-4">
<div class="card">
<div class="card-body">
<b>Jaula:</b> {{name}} <b>Animal specie:</b> {{animalspecie.name}} <a href="/editcage/{{_id}}"><button class="btn btn-warning mt-3">Edit</button></a> <a href="/removecage/{{_id}}"><button class="btn btn-danger mt-3">Remove</button></a>
</div>
</div>
</div>
{{/each}}
<h2 class="mt-4">Cages</h2>
{{#each cages}}
<div class="col-md-12 mt-4">
<div class="card">
<div class="card-body">
<b>Jaula:</b> {{name}} <b>Animal specie:</b> {{animalspecie.name}} <a href="/editcage/{{_id}}"><button class="btn btn-warning mt-3">Edit</button></a> <a href="/removecage/{{_id}}"><button class="btn btn-danger mt-3">Remove</button></a>
</div>
</div>
</div>
{{/each}}
我只能获得笼子ID,但是笼子.hbs中没有动物种类。 知道我该如何解决吗? 谢谢!