【问题标题】:push value of one schema into another schema将一个模式的值推入另一个模式
【发布时间】: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


    【解决方案1】:

    在我看来,您应该在图像架构中添加用户名和电子邮件字段。然后设置 required = false,如果不是每种情况都需要,或者您可以通过在模式中设置严格 = false 来添加它而不自定义模式。它允许您保存或更新您的案例中的文档。

    这是显示如何设置 strict = false 的答案 https://stackoverflow.com/a/50935227/15072782

    【讨论】:

      【解决方案2】:

      您可以使用参考文档概念来更改图像架构。这样您就可以只存储用户 ID。

      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: {                                      // update here
            type: Schema.Types.ObjectId, 
            ref: 'User' 
          }
      });
      module.exports = model('Image', imageSchema);
      
      
      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; // 我应该在这里做什么 await image.save(); res.redirect('/user/feed'); }); ```
      • 从帖子正文本身,您可以发送用户 id 并代替authername = req.body.authername
      • 我用这个 image.authorname=req.body.authorname 替换了评论;我检查了 db 用户 id 没有被保存,db 显示所有信息,但没有关于作者名的信息我想我在这里不明白
      • 你能给我看一下帖子正文吗?
      • 你的意思是ejs文件吗?还是上面提到的函数的post函数?
      猜你喜欢
      • 2019-07-18
      • 1970-01-01
      • 2016-03-18
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多