如何在子集合中创建虚拟对象 - Mongoose
How to make a virtual object in a subcollection - Mogoose
在 mongoose 中,我有一个带有图像(名称+分机号)的对象 ('target')。每个对象 ('target')。有一个其他对象的子集合,该子集合再次包含图像(名称+分机)。
现在我已经能够为 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
});
在 mongoose 中,我有一个带有图像(名称+分机号)的对象 ('target')。每个对象 ('target')。有一个其他对象的子集合,该子集合再次包含图像(名称+分机)。
现在我已经能够为 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
});