我的问题是我想加入此架构。例如:在任何电子商务网站中都有一个主图片,点击该产品后,我们可以看到该产品的多张图片。要上传图片,我使用 multer 包,在multer中,我们无法以相同的形式上传单个图片和图片数组,因此我创建了一个新表单只需要一个图像数组。所以我想将图像数据数组与我的主表单连接起来
此架构用于上传单个图像:
const mongoose = require('mongoose');
const singleImageSchema = new mongoose.Schema({
file: {
type: mongoose.Schema.Types.ObjectId,
ref: 'File',
},
singleImage: {
type: String,
},
});
module.exports = new mongoose.model('File', singleImageSchema);
此架构用于上传多张图片:
const mongoose = require('mongoose');
const multipleImageSchema = new mongoose.Schema({
multipleImage: {
type: [String],
},
});
module.exports = new mongoose.model('Image', multipleImageSchema);
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const mongoose = require('mongoose');
const multer = require('multer');
const path = require('path');
const helpers = require('./helpers');
const singleImage = require('./models/singleImage');
const multipleImage = require('./models/multipleImage');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/uploads');
},
// By default, multer removes file extensions so let's add them back
filename: function (req, file, cb) {
cb(
null,
file.fieldname + '-' + Date.now() + path.extname(file.originalname),
);
},
});
const app = express();
app.set('view engine', 'ejs');
app.use(
bodyParser.urlencoded({
extended: true,
}),
);
app.use(express.static('public'));
//connect to database
mongoose.connect('mongodb://localhost:27017/ImageInDB', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log('Database is connected successfully on port 27017!!!');
});
app.get('/', (req, res) => {
res.render('file_upload');
});
app.post('/upload-profile-pic', (req, res) => {
let upload = multer({
storage: storage,
fileFilter: helpers.imageFilter,
}).single('profile_pic');
upload(req, res, function (err) {
if (req.fileValidationError) {
return res.send(req.fileValidationError);
} else if (!req.file) {
return res.send('Please select an image to upload');
} else if (err instanceof multer.MulterError) {
return res.send(err);
} else if (err) {
return res.send(err);
}
const oneImage = new singleImage({
singleImage: req.file.filename,
});
oneImage.save(err => console.log(err));
// Display uploaded image for user validation
singleImage.find({}, (err, product) => {
res.render('preview', {
product: product,
});
});
});
});
app.post('/upload-multiple-images', (req, res) => {
let upload = multer({
storage: storage,
fileFilter: helpers.imageFilter,
}).array('multiple_images', 10);
upload(req, res, function (err) {
if (req.fileValidationError) {
return res.send(req.fileValidationError);
} else if (!req.files) {
return res.send('Please select an image to upload');
} else if (err instanceof multer.MulterError) {
return res.send(err);
} else if (err) {
return res.send(err);
} // The same as when uploading single images
const files = req.files;
const moreImage = new multipleImage({
multipleImage: files.map(file => file.filename),
});
moreImage.save(err => console.log(err));
// Loop through all the uploaded images and display them on frontend
multipleImage.find({}, (err, products) => {
res.render('multiPreview', {
products: products,
});
});
});
});
app.listen(3000, function () {
console.log('Server started on port 3000');
});
答案 0 :(得分:0)
您需要将引用架构添加到singleImageSchema,然后使用populate()方法,如下所示:
const singleImageSchema = new mongoose.Schema({
file: {
type: mongoose.Schema.Types.ObjectId,
ref: 'File',
},
singleImage: {
type: String,
},
multipleImage: [{ type: Schema.Types.ObjectId, ref: 'Image' }]
});
然后您的查询将变为
singleImage
.find()
.populate('multipleImage')
.exec(function(err, result) {
});
populate()方法的文档:https://mongoosejs.com/docs/populate.html
为什么不将其添加到单个表中?