简体   繁体   English

如何进行快速路由,使mysql删除特定ID的查询

[英]How to make an express route that makes a mysql delete query for a specific ID

I'm trying to make a post request in express that when fired, deletes a row in my Tasks MySQL table, which is connected to a specific user ID. 我试图发出一个表达要求,即当被触发时,删除我的Tasks MySQL表中的一行,该表连接到特定的用户ID。 I'm not sure how to go about it... I think I have to use req.params.id in my code somewhere, but I don't know if thats all I need or if thats even right to begin with. 我不确定该怎么做...我想我必须在我的代码中的某处使用req.params.id,但是我不知道这是否就是我所需要的,还是那才是正确的开始。

Here is what I have so far: 这是我到目前为止的内容:

const express = require("express");
const app = express();
const mysql = require("mysql");
const bodyParser = require("body-parser");

app.use(bodyParser.urlencoded({extended: true}));

const connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "PASSWORD",
        database: "DATABASE"
    });

connection.connect(function(err) {
    if (err) {
        console.log("Your connection to the database failed \n");
    } else {
        console.log("Your connection to the database was successful \n")
    }
});

app.post("/deleteTask", function(req, res) {
    let remove = "DELETE FROM Tasks WHERE "; // finish the query

    connection.query(remove, function(err, result) {
        if (err) {
            console.log("The delete query failed");
            res.sendStatus(500);
            throw err;
        } else {
            res.sendStatus(200);
            console.log(result);
        }
        connection.end();
    });
});

Yes, first I suggest you to read the doc about routing : https://expressjs.com/en/guide/routing.html 是的,首先我建议您阅读有关路由的文档: https : //expressjs.com/en/guide/routing.html

After If it was my jobs I'll get the taskId with something like 之后,如果这是我的工作,我将获得类似的taskId

app.post("/deleteTask/:taskId", function(req, res) {
    const taskId = req.params.taskId;

    let remove = "DELETE FROM Tasks WHERE "; // finish the query
    ...
});

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

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