1

So I'm working with NodeJS, MongoDB and ReactJS.

I have a Task List where I add -(Task.create) with mongoose- to my project multiple tasks and I have a form with a button to delete those tasks, but I can't do it because I can't access to each task ID to delete it.

Here's my server:

(This is wrong, "task is not defined" but I don't know how to get each task id)

exports.delete_task= (req, res) => {

Task.findByIdAndDelete({tasks: task._id}, (err) =>{
    if(err){
        console.log(err);
    }
 })
};  

Here's my (reactjs) Tasks Component

 import React, { Component } from 'react';
 import { NavLink } from 'react-router-dom';
 import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
 import { faTrashAlt, faEdit } from '@fortawesome/free-solid-svg-icons'

 import './Tasks.css';

 class Tasks extends Component {
    constructor(props) {
        super(props);
        this.state = {
            projectId: props._id,
            tasks: []
        };
    }

 componentDidMount() {
    fetch(`/dashboard/project/${this.props.projectId}/tasks`)
        .then(response => {
            return response.json()
        }).then(task => {
            this.setState({
                tasks: task.tasks
            })
        }).catch(error => console.error('Error:', error));
 }

 render() {

    const { tasks } = this.state;
    return (
        <div>
                <ul className="task-list">
                {tasks.map(task =>
                <li key={task._id}>{task.tasktitle} 

               <div>

              <form method='POST' action={`/dashboard/project/${this.props.projectId}/tasks/delete?_method=DELETE`}>
                 <button className="btn button--tasks" >
                    <FontAwesomeIcon icon={faTrashAlt} />
                 </button>
                </form>
                   </div>
                     </li>
                )}
                </ul>        
      </div>  
    );
  }
}

export default Tasks;

enter image description here

Here's an image of my task list, where I can ADD but can't DELETE it because I can't access to each task ID by clicking its own trash button..

Hope you can help me. Thank you so much!!

2 Answers 2

1

Do it like below. While doing .map pass task._id as query param to the path like below

{tasks.map(task =>
                <li key={task._id}>{task.tasktitle} 

               <div>

              <form method='POST' action={`/dashboard/project/${this.props.projectId}/tasks/delete?_method=DELETE&taskId=${task._id}`}>
                 <button className="btn button--tasks" >
                    <FontAwesomeIcon icon={faTrashAlt} />
                 </button>
                </form>
                   </div>
                     </li>
                )}


exports.delete_task= (req, res) => {

And here get the task id using req.query.taskId

Task.findByIdAndDelete({tasks: req.query.taskId}, (err) =>{
    if(err){
        console.log(err);
    }
 })
};
Sign up to request clarification or add additional context in comments.

4 Comments

Wow thanks! But now I receive a "Cannot POST /dashboard/project/5babee3bba94ea38bfbccd7f/tasks/delete" and my localhost is like this: localhost:5000/dashboard/project/5babee3bba94ea38bfbccd7f/tasks/… .. maybe I have something wrong in routes or something?
May I know where do you get that error I mean front end or backend?
Not sure how form action works may be you need to specify path along with localhost:5000/dashboard in action
my bad, I was missing a letter. Thank you so much for your help!!
1

You need to pass the task id as is findByIdAndDelete(taskId) which is same as findByIdAndDelete({'_id': task._id})

Task.findByIdAndDelete(task._id, (err) =>{
    if(err){
        console.log(err);
    }
 })
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.