在猫鼬中,我有一个带有图像(名称+扩展名)的对象('目标')。每个对象(“目标”)。具有其他对象的子集合,该子集合再次包含图像(名称+扩展名)。
现在,我已经能够为target.image创建虚拟图像。所以我得到了一个完整的位置URL。但是我无法为target.upload.image创建虚拟。
我该怎么做?
const targetSchema = mongoose.Schema({
title: {
type: String, required: true
},
userId: {
type: String, required: true
},
........
image: {
type: String, required: true
},
uploads: [{
upload_image: {
type: String, required: true
},
upload_userId: {
type: String, required: true
},
}],
});
这是我的目标图片的虚拟图片
targetSchema.virtual('targetImgURL').get(function(){
let url = process.env.URL + ":" + process.env.PORT + "/public/images/targets/uploads/" + this.image
return url
});
是,我已经找到了解决方案。不幸的是,不可能一次扣留所有东西。但是这种方式并不麻烦。请参阅下面的代码以获取解决方案。
const uploadSchema = mongoose.Schema({
upload_image: {
type: String, required: true
},
upload_userId: {
type: String, required: true
},
});
// Virtuals
uploadSchema.virtual('UploadImgURL').get(function(){
let url = process.env.URL + ":" + process.env.PORT + "/public/images/targets/answers/" + this.image
return url
});
const targetSchema = mongoose.Schema({
title: {
type: String, required: [true, 'A title is required']
},
....
uploads: [ uploadSchema ],
});
// Virtuals
targetSchema.virtual('targetImgURL').get(function(){
let url = process.env.URL + ":" + process.env.PORT + "/public/images/targets/uploads/" + this.image
return url
});