【发布时间】:2022-01-15 15:28:14
【问题描述】:
我有一个文件 ./models/Image.js
const { Schema, model } = require('mongoose');
const imageSchema = new Schema({
title: {type: String},
description: {type: String},
author:{type:String},
filename: {type: String},
path: {type: String},
originalname: {type: String},
mimetype: {type: String},
size: { type: Number},
created_at: {type: Date, default: Date.now()},
authorname:{type:String}
});
module.exports = model('Image', imageSchema);
我还有另一个文件 ./models/User.js
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name :{type:String,required : true} ,
email :{type : String,required : true} ,
password :{type : String,required : true} ,
date :{type : Date,default : Date.now}
});
const User= mongoose.model('User',UserSchema);
module.exports = User;
和路由/用户内部的函数
router.post('/upload', async (req, res) => {
const image = new Image();
image.title = req.body.title;
image.description = req.body.description;
image.filename = req.file.filename;
image.path = '/img/uploads/' + req.file.filename;
image.originalname = req.file.originalname;
image.mimetype = req.file.mimetype;
image.size = req.file.size;
//image.authoremail= User.req.email; // what should i do here
await image.save();
res.redirect('/user/feed');
});
我想要的是将用户名和电子邮件放入图像架构中,以便我可以比较它以供以后使用 例如,在仪表板用户的页面中,仅显示他/她上传的图片,但在页面内“提要”所有用户的图片都显示有各自的名称
【问题讨论】:
标签: javascript java node.js mongodb mongoose