Mern, Issue with Removing Comments

Having trouble deleting comments within a single post? I keep encountering a 404 Error and I'm not sure why the post is not being found. I really need some assistance in figuring out what's causing this issue and why I'm unable to delete a comment from a post. Below, I'll provide some backend and frontend code snippets, along with a link to my Github repository. I've been struggling with this for almost 3 days now and would greatly appreciate any help. One thing to note is that I'm fairly new to MERN, so please take a look at my Github code and feel free to suggest any improvements or changes you think might be helpful. Below you'll find some code snippets and a link to my Github Repo for this specific project

https://github.com/tigerabrodi/ELance

Route for Deleting a Comment from a post


// @route    DELETE api/posts/comment/:commentId 
// @desc     Delete comment
// @access   Private
router.delete("/comment/:commentId", auth, async (req, res) => {
  try {
    const comment = await Comment.findById(req.params.commentId);

    if (!comment) {
      return res.status(404).json({ msg: "Comment do not exist" });
    }

    // more code here...

});

Post.js

import React, { Fragment, useEffect } from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import Spinner from '../layout/Spinner';
import PostItem from '../posts/PostItem';
// more code here...

CommentItem.js

import React from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import Moment from 'react-moment';
import {deleteComment} from '../../redux/comment/comment.actions';
import defaultUserImage from "../../assets/default-user-icon.jpg"
// more code here...

CommentForm.js

import React, { useState } from 'react';
import { connect } from 'react-redux';
import { addComment } from '../../redux/comment/comment.actions';
// more code here...

comment.actions.js

import axios from "axios";
import {setAlert} from "../alert/alert.actions"
import {CommentActionTypes} from "./comment.types"
// more code here...

comment.reducer.js

import {CommentActionTypes} from "./comment.types";

const initialState = {
    comments: [],
    loading: true,
    error: {}
}
// more code here...

comment.types.js

export const CommentActionTypes = {
    DELETE_COMMENT: "DELETE_COMMENT",
    ADD_COMMENT: "ADD_COMMENT",
    COMMENT_ERROR: "COMMENT_ERROR",
    GET_SINGLE_POST_COMMENTS: "GET_SINGLE_POST_COMMENTS"
}

Answer №1

To properly implement the deleteComment method in comment.actions.js, make sure to include a forward slash at the beginning of the delete URL:

    await axios.delete(`/api/comments/delete/${commentId}`);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for creating a seamless merge from background color to a pristine white hue

Seeking a seamless transition from the background color to white at the top and bottom of the box, similar to the example screenshot. Current look: The top and bottom of the box are filled with the background color until the edge https://i.stack.imgur.com ...

Enhancing search results with data retrieved from a jSON response

At the moment, I am using a logic to generate a list of titles. Now, I would like to include data from the response along with the code for each title in the list. const title = responseData.map(item => { return { label: item.title, val ...

Maintaining consistent row color selection in Material UI even when another row is selected

I am currently working with the Material-UI Collapsible table feature. My goal is to change the color of the selected row, and I have achieved this by applying the following CSS: const styles = theme => ({ tableRow: { '&.Mui-selected, &am ...

What is the best way to check the API response status in NextJS13?

Currently, I am experimenting with different methods to handle API HTTP status in my NextJS-13 project but so far nothing has been successful. Note: TypeScript is being used in this project. Below is my code snippet with a static 200 API response and the ...

Firestore version 9 - Retrieve nested collection depending on a string being present in an array

Working on a new chat application here. It has been a while since I last used Firestore. I am currently using v9 and facing an issue with retrieving the nested "messages" collection when the "users" array in the document contains a specific ID. I have man ...

"Using nginx to proxy a Node.js Express application running on a remote server within a sub

I am facing a challenge where I need to host multiple node applications on a single server. To achieve this, I have set up the applications to run on different ports and can access them by specifying the IP address along with the respective port numbers. ...

Pass information between two components in React

I have come across several similar questions to this one, but I am having trouble understanding their solutions. I have a component that fetches data and now I want to pass this data along with other information to another component (is it a child compone ...

Learn how to incorporate the prettier command into your coding workflow by adding it as a devDependency before committing code or

I am currently engaged in a React project. I am looking to set up autoformatting on save in my Visual Studio Code. However, I prefer not to adjust the settings within VSCode itself due to the variation in configurations among users. Instead, I want to ach ...

Unable to retrieve information from localhost site using the expressjs API. I have attempted to use both vue-resource and axios in order to get the data without success

Currently diving into the world of VueJS, I decided to embark on a project. My aim is to retrieve data from an ExpressJS server/API. But unfortunately, both vue-resource and axios have been returning status code 0. It seems like my API might not be handli ...

Changing the State of a CheckBox with ReactJS and Material UI

Utilizing Material UI's Checkbox, I am creating a form to input data and update or add values into a state object. This form serves the purpose of both editing an existing holiday or adding a new one. I'm currently facing an issue where the stat ...

Error encountered when attempting to reference multiple Select MenuItems in Material UI

Recently, I've been encountering a perplexing error when attempting to open a multiple Select in React+Next.js using Material UI: Error: Argument appears to not be a ReactComponent. Keys: retry This issue seems to be related to a ref. It occurs with ...

Is there a way to transmit React code using Express?

Currently, I'm in the process of creating a coloring demonstration. Initially, I had an SVG file at hand, but I made the decision to utilize svgr for its conversion into a React component. This strategy will offer me the flexibility to easily modify i ...

Tips for preventing a React component from scrolling beyond the top of the page

I am looking to achieve a specific behavior with one of my react components when a user scrolls down a page. I want the component to reach the top of the page and stay there without moving any further up. Here is an Imgur link to the 'intranet' ...

Tips on successfully utilizing inner refs within a Higher Order Component

In this scenario, imagine having a custom input component alongside another wrapper component named StyledInput. The goal is to transfer the internal reference of the Input through the StyledInput, while simultaneously being able to access and apply the s ...

Every time a request is made, an enormous item is dropped

Recently, while working on my express.js application on my laptop, I noticed a peculiar behavior. Upon each request, a huge JSON object is printed to the console. The object begins like this: { domain: null, _events: null, _maxListeners: 10, socket: ...

bring in the router using express.Router()

I have this at the beginning of my index.js file: import express from 'express' import favicon from 'serve-favicon' import router from express.Router() However, I kept encountering the following error messages: [nodemon] app crashed ...

What are the steps for activating HTML syntax highlighting in Visual Studio Code when working on an .ntl template file?

Before, I used Sublime Text editor and the HTML syntax highlighting worked automatically for .ntl files. However, after installing Visual Studio Code, there seems to be no syntax highlighting applied to an .ntl file. I found a solution on how to manually ...

Looking for guidance on sending data from a JS file to HTML using Nodejs? Seeking advice on various modules to achieve this task effectively? Let's

Looking for advice on the most effective method to transfer data from a JS file (retrieved from an sqlite db) to an HTML file in order to showcase it in a searchable table. My platform of choice is NodeJS. As a beginner, I am willing to put in extra time a ...

Clear the tag from occupied Calendar Material UI on the desktop

I am trying to customize the appearance of the label in the DesktopDatePicker component in Material UI. By removing the label prop, I was able to achieve my desired result, however, I noticed that the placeholder text is now positioned lower than expected. ...

Is it possible to trigger the onNewRequest property when the onBlur event is fired for AutoComplete in Material-UI?

Currently, I am utilizing Material-UI and making use of the onNewRequest property in the AutoComplete field. However, the onNewRequest only triggers when Enter is pressed or when a value is selected. I am interested in calling the onNewRequest even when ...