简体   繁体   English

无法使用ReactJs MongoDB NodeJS删除项目

[英]Can't delete item using ReactJs MongoDB NodeJS

So I'm working with NodeJS, MongoDB and ReactJS. 所以我正在使用NodeJS,MongoDB和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. 我有一个任务列表,其中向我的项目中添加了-(Task.create)和猫鼬-多个任务,并且我有一个带有删除这些任务的按钮的表单,但是我不能这样做,因为我无法访问每个任务任务ID将其删除。

Here's my server: 这是我的服务器:

(This is wrong, "task is not defined" but I don't know how to get each task id) (这是错误的,“未定义任务”,但我不知道如何获取每个任务ID)

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

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

Here's my (reactjs) Tasks Component 这是我的(reactjs)任务组件

 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;

在此处输入图片说明

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.. 这是我的任务列表的图像,我可以在其中添加但不能删除它,因为我无法通过单击其自己的垃圾箱按钮来访问每个任务ID。

Hope you can help me. 希望您能够帮助我。 Thank you so much!! 非常感谢!!

You need to pass the task id as is findByIdAndDelete(taskId) which is same as findByIdAndDelete({'_id': task._id}) 您需要像findByIdAndDelete(taskId)一样传递任务ID,它与findByIdAndDelete({'_id': task._id})

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

Do it like below. 像下面那样做。 While doing .map pass task._id as query param to the path like below 在执行.map时,将task._id作为查询参数传递给如下所示的路径

{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 然后在这里使用req.query.taskId获取任务ID

Task.findByIdAndDelete({tasks: req.query.taskId}, (err) =>{
    if(err){
        console.log(err);
    }
 })
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM