MERN Q: Implementation of node.js function that checks if a user who is accessing a post is the individual who posted the post in the first place


I am looking at writing out a node.js function that allows you to “supervote” or “star” a specific answer/response to YOUR question. You are the only one allowed to do this if the original post was made by you. I would prefer an if statement if possible. Below is the code I have at the moment but it does not do what I want. Thank you in advance. The module FEED.js file works with all other functions and calls, so this is the only issue I have. The issue I have is at the "if(feedpost.User.id === postW)". Not sure how to implement this check!

// @route       PUT api/feed/supervote/user/:id           //vote on the best response to YOUR post
// @description vote on best response
// @access      Private        

router.put('/comment/supervote/:id', auth, async (req,res) =>{                   
    try{

            const feedpost = await Feed.findById(req.params.id);
            const postW = await User.findById(req.params.id);       //gets id of user
            // Check if the post has been voted on by this user
           
            
            if(feedpost.User.id === postW){ 
            

            if(feedpost.supervote.filter(supervote => supervote.user.toString() === req.user.id).length > 0){          //if greater than 0, then theyve used their best answer vote

                    return res.status(400).json({msg: 'You have exceeded your limit of best answer votes for this post'});
            }
            feedpost.supervote.unshift({user: req.user.id});

            await feedpost.save();              //saves this value back into the database linked to the post id

            res.json(feedpost.supervote);


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


/////////////////
User.js
////////////////
const mongoose = require('mongoose');

const UserSchema = new mongoose.Schema({ //user schema to define what the user has to enter for registration + is it required and what condition (for password).
    name:{
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
        unique: true
    },
    password:{
        type: String,
        required: true
    },
    avatar:{
        type: String
    },
    date: {
        type: Date,
        default: Date.now
    }
});

module.exports = User = mongoose.model('user', UserSchema);

///////////////////////////
Feed.js
//////////////////////////
const mongoose = require ('mongoose');


const FeedSchema = new mongoose.Schema({



    user: {
        type: mongoose.Schema.Types.ObjectID,          //connects post to user
        ref: 'user'
    },

    text: {
        type: String,
        required: true
    },

    name: {             //name of the user so we can keep post if the account is deleted
        type: String
    },
    avatar:{
        type: String
    },
    vote: [
        {
            user: {
                type: mongoose.Schema.Types.ObjectID,            // user can only vote up or down on a post once. Number cannot increase
                ref: 'user'
            }
        }
    ],
    supervote:[

        {
            user: {
                type: mongoose.Schema.Types.ObjectID,
                ref: 'user'
            }

        }



    ],
    
    comments: [
        {
            user: {
                type: mongoose.Schema.Types.ObjectID,         
                ref: 'user'
            } ,
            text: {
                type: String,
                required: true

            },
            avatar: {
                type: String
            },
            date: {
                type: Date,
                default: Date.now
            },
        }
    ],
    date: {
        type: Date,
        default: Date.now
    }
});

module.exports = Feed = mongoose.model('feed',FeedSchema );

Source: JavaSript – Stack Overflow

November 6, 2021
Category : News
Tags: encoding | javascript | node.js | vote

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.