Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return the user email

I have created post route to store posts in the database. It's a protected route so user can store post only after entering the login details. When I post in postman, I've seen that the user email is not returned in the object. Even in the mongodb collection, I don't see the email associated with the post. How do I include the email as well with the post object. I don't want the user to enter the email again and again when posting because they have already logged in. So I kinda want to store the email automatically with the post. Hope I make sense. Can someone help me with this?

Right now the object is kinda stored like this in the posts collection in mongodb

_id: ObjectId("5f1a99d3ea3ac2afe5"),
text: "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. ",
user:ObjectId("5f1a99d3eac2c82afe5"),
age:20,
country:"India",
gender:"male",
date:2020-07-24T08:23:35.349+00:00,
__v:0

I want the email too in the above object.

Post model

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const PostSchema = new Schema ({
    user: {
        type: Schema.Types.ObjectId,
        ref: 'users'
    },
    text: {
        type: String,
        required: true
    },
    name: {
        type: String
    },
    
    email: {
            type: String
    }
    ,
    age: {
        type: Number,
        required: true
    },
    gender: {
        type: String,
        required: true
    },
    country: {
        type: String,
        required: true
    },
    date: {
        type: Date,
        default: Date.now
    }
})

module.exports = Post = mongoose.model('post', PostSchema)

post route

const express = require('express');
const router =  express.Router();
const auth = require('../../middleware/auth')
const { check, validationResult} = require('express-validator');
const User = require('../../models/User')
const Post = require('../../models/Post')
router.post('/', [auth, [
    check('text', 'Text is required').not().isEmpty()
]], async (req,res)=>{
    const errors = validationResult(req);
    if(!errors.isEmpty()){
        return res.status(400).json({errors: errors.array()})
    }

   
    try {
        const user = await (await User.findById(req.user.id)).isSelected('-password')

    const newPost = new Post({
        text: req.body.text,
        name: user.name,
        user: req.user.id,
        age: req.body.age,
        country: req.body.country,
        gender: req.body.gender,
        email: req.user.email // this email is not stored with the post and I want this to be automatically posted in the collection without the user having to type it again to save the post
    })
    const post = await newPost.save();
    res.json(post);

    } catch (err) {
        console.error(err.message);
        res.status(500).send('Server Error')
    }
})

module.exports = router;

User model

const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },
    date: {
        type: Date,
        default: Date.now
    }
})

module.exports = User = mongoose.model('user', UserSchema);
like image 508
helpmepiliizzz Avatar asked Dec 19 '25 04:12

helpmepiliizzz


1 Answers

Change isSelected to select

const user = await (await User.findById(req.user.id)).isSelected(password')

like image 131
prax Avatar answered Dec 21 '25 19:12

prax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!