在父级中访问猫鼬的子方法在数组中成功,并在单个子项中失败
更新:解决方案在问题的底部
我有一个使用猫鼬的快速站点。
我将简化为说我有成人,孩子和房屋模型。当我在孩子上创建方法时,我可以在成人的方法内调用它们并获得结果。我也可以从.ejs视图中调用它们。但是,当我在内部创建方法时,只能从.ejs视图中获取结果,而在成人的方法内部调用时,则无法得到定义。示例代码如下。
adult.js
const mongoose = require('mongoose');
const adultSchema = mongoose.Schema({
name: { type: String },
size: {type: String},
kids: [{type: mongoose.Schema.Types.ObjectId, ref: 'Kid', required: true}]
house:{type: mongoose.Schema.Types.ObjectId, ref: 'House', required: true}
});
adultSchema.method({
getKidsDescription: function() {
if (this.kids.length < 1) {
return 'No kids yet';
} else {
let ev = 'Kids, aged: ';
let kds = this.kids;
kds.forEach(function(k){
ev = ev + 'k.getAge()' // works
})
return ev;
}
},
getHouseDescription: function(){
return 'A fabulous house on '+this.house.getFullStreet(); // does not work
}
})
module.exports = mongoose.model('Adult', adultSchema);
kid.js
const mongoose = require('mongoose');
const kidSchema = mongoose.Schema({
name: { type: String },
size: {type: String},
birthdate: {type:Date}
});
kidSchema.method({
getAge: function() {
return (Math.floor(new Date() - this.birthdate)/(1000*60*60*24*365))
},
})
module.exports = mongoose.model('Kid', kidSchema);
house.js
const mongoose = require('mongoose');
const houseSchema = mongoose.Schema({
name: { type: String },
city: {type: String},
street: {type:String}
});
houseSchema.method({
getFullStreet: function() {
return this.street + ' Road';
},
})
module.exports = mongoose.model('House', houseSchema);
当我查询成人时,它看起来像这样:
controller.js
exports.main = async (req, res, next) => {
if (req.theAdult) {
try {
const found = await db.fetchAdult(req.theAdult._id)
res.render('/main', {
//theHouse: found.house //below I show this working
});
} catch(e) {
throw new Error(e.message)
}
} else {
res.redirect('/');
}
}
db.js
exports.fetchAdult = (id) => {
return Adult.findById(id)
.populate({ path: 'kids'})
.populate({ path: 'house'})
.exec()
.then(doc => {
return doc;
});
}
假定房屋在渲染时作为对象传递给视图(上面注释),此方法有效
view.ejs
<p> <%= theHouse.getFullStreet() %></p>
假定在填充成人的呼叫中填充了房屋,则返回未定义。
view.ejs
<p> <%= theAdult.house.getFullStreet() %></p>
同时,这两项工作
view.ejs
<ul> <% theAdult.kids.forEach(function(k) { %>
<li><%= k.getAge() %> </li>
<% }); %>
</ul>
<p> <% theAdult.getKidsDescription() %> </p>
我不明白的是,该方法调用如何对数组中的对象起作用,并在视图中起作用,但不适用于数组中的对象。 (对我来说)这是一个单子错误。如果它在视图中不起作用,那么我认为方法getFullStreet()
是问题所在,但在视图中有效。如果无法在父级中调用数组方法,则可以认为问题在于尝试访问父级中的getFullStreet()
。
我想念什么?
SOLUTION我在显示show.ejs的调用中获取了theAdult
,但实际上我实际上依赖的是currentAdult
,它引用了req.adult
,并且没有填充字段。我的解决方案是向成人模式添加一个预钩子,该钩子总是在查找时填充房屋。
in Adult.js
adultSchema.pre('find', function() {
this.populate('house')
})
回答如下:您是否尝试过让成人喝水?它可能只看到ObjectID,而没有任何其他数据或方法。
在父级中访问猫鼬的子方法在数组中成功,并在单个子项中失败
更新:解决方案在问题的底部
我有一个使用猫鼬的快速站点。
我将简化为说我有成人,孩子和房屋模型。当我在孩子上创建方法时,我可以在成人的方法内调用它们并获得结果。我也可以从.ejs视图中调用它们。但是,当我在内部创建方法时,只能从.ejs视图中获取结果,而在成人的方法内部调用时,则无法得到定义。示例代码如下。
adult.js
const mongoose = require('mongoose');
const adultSchema = mongoose.Schema({
name: { type: String },
size: {type: String},
kids: [{type: mongoose.Schema.Types.ObjectId, ref: 'Kid', required: true}]
house:{type: mongoose.Schema.Types.ObjectId, ref: 'House', required: true}
});
adultSchema.method({
getKidsDescription: function() {
if (this.kids.length < 1) {
return 'No kids yet';
} else {
let ev = 'Kids, aged: ';
let kds = this.kids;
kds.forEach(function(k){
ev = ev + 'k.getAge()' // works
})
return ev;
}
},
getHouseDescription: function(){
return 'A fabulous house on '+this.house.getFullStreet(); // does not work
}
})
module.exports = mongoose.model('Adult', adultSchema);
kid.js
const mongoose = require('mongoose');
const kidSchema = mongoose.Schema({
name: { type: String },
size: {type: String},
birthdate: {type:Date}
});
kidSchema.method({
getAge: function() {
return (Math.floor(new Date() - this.birthdate)/(1000*60*60*24*365))
},
})
module.exports = mongoose.model('Kid', kidSchema);
house.js
const mongoose = require('mongoose');
const houseSchema = mongoose.Schema({
name: { type: String },
city: {type: String},
street: {type:String}
});
houseSchema.method({
getFullStreet: function() {
return this.street + ' Road';
},
})
module.exports = mongoose.model('House', houseSchema);
当我查询成人时,它看起来像这样:
controller.js
exports.main = async (req, res, next) => {
if (req.theAdult) {
try {
const found = await db.fetchAdult(req.theAdult._id)
res.render('/main', {
//theHouse: found.house //below I show this working
});
} catch(e) {
throw new Error(e.message)
}
} else {
res.redirect('/');
}
}
db.js
exports.fetchAdult = (id) => {
return Adult.findById(id)
.populate({ path: 'kids'})
.populate({ path: 'house'})
.exec()
.then(doc => {
return doc;
});
}
假定房屋在渲染时作为对象传递给视图(上面注释),此方法有效
view.ejs
<p> <%= theHouse.getFullStreet() %></p>
假定在填充成人的呼叫中填充了房屋,则返回未定义。
view.ejs
<p> <%= theAdult.house.getFullStreet() %></p>
同时,这两项工作
view.ejs
<ul> <% theAdult.kids.forEach(function(k) { %>
<li><%= k.getAge() %> </li>
<% }); %>
</ul>
<p> <% theAdult.getKidsDescription() %> </p>
我不明白的是,该方法调用如何对数组中的对象起作用,并在视图中起作用,但不适用于数组中的对象。 (对我来说)这是一个单子错误。如果它在视图中不起作用,那么我认为方法getFullStreet()
是问题所在,但在视图中有效。如果无法在父级中调用数组方法,则可以认为问题在于尝试访问父级中的getFullStreet()
。
我想念什么?
SOLUTION我在显示show.ejs的调用中获取了theAdult
,但实际上我实际上依赖的是currentAdult
,它引用了req.adult
,并且没有填充字段。我的解决方案是向成人模式添加一个预钩子,该钩子总是在查找时填充房屋。
in Adult.js
adultSchema.pre('find', function() {
this.populate('house')
})
回答如下:您是否尝试过让成人喝水?它可能只看到ObjectID,而没有任何其他数据或方法。