Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
push value of one schema into another schema

i have a file ./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);

I have another file ./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;

and a function inside routes/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');
});

What i want is to put users name and email inside image schema so that i can compare it for later purposes for example in a page of dashboard user is only shown the picture he/she has uploaded but inside page 'feed' pictures of all users is displayed with there respective name

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

In my opinion you should add user name and email field in the image schema. then set required = false, if it's not required for every case or you can add it without custom the schema by set strict = false in schema. it allow you to save or update the document in your case.

this is the answer that show how to set strict = false https://stackoverflow.com/a/50935227/15072782

about 4 years ago · Santiago Gelvez Relatório

0

You can change the image Schema by using,reference document concept. So that you can just store user 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;
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda