我在FE上没有框架的情况下使用JS创建了看板。在后端,我正在使用Express / Postgres。
我的问题是我的“删除”操作不起作用。所有其他CRUD操作都可以。
当我使用该应用尝试删除“计划”时,请求将返回状态“ 200”代码,但该条目实际上并未被删除。
我的代码在下面-
Queries.js
const Pool = require('pg').Pool;
const pool = new Pool({
user: 'kanban',
host: 'localhost',
database: 'kanbandb',
password: 'k4nb4n.d3',
port: 5432
});
const getPlans = (request, response) => {
pool.query(
'SELECT * FROM plans ORDER BY project_id ASC',
(error, results) => {
if (error) {
throw error;
}
response.status(200).json(results.rows);
}
);
};
const getPlanById = (request, response) => {
const id = parseInt(request.params.id);
pool.query('SELECT * FROM plans WHERE id = $1', [id], (error, results) => {
if (error) {
throw error;
}
response.status(200).json(results.rows);
});
};
const createPlan = (request, response) => {
console.log(request.body);
const planID = request.body.planID;
const planName = request.body.planName;
// const barColor = request.body.barColor;
const checkbox_state = true;
pool.query(
'INSERT INTO plans (plan_id, plan_name, checkbox_state) VALUES ($1, $2, $3)',
[planID, planName, checkbox_state],
(error, results) => {
if (error) {
throw error;
}
console.log(results);
response.status(201).send(`Plan added with ID: ${results.insertId}`);
}
);
};
const updatePlan = (request, response) => {
//const id = parseInt(request.params.id);
const { planID, planName, barColor, checkbox_state } = request.body;
pool.query(
'UPDATE plans SET plan_name = $1, plan_id = $2 WHERE id = $3',
[planID, planName, barColor, checkbox_state],
(error, results) => {
if (error) {
throw error;
}
response.status(200).send(`Plan modified with ID: ${id}`);
}
);
};
const deletePlan = (request, response) => {
console.log(request.body);
const planName = request.body.planName;
pool.query(
'DELETE FROM plans WHERE plan_name = $1',
[planName],
(error, results) => {
if (error) {
throw error;
}
console.log(results);
response.status(200).send(`Plan deleted with Name: ${results}`);
}
);
};
module.exports = {
getPlans,
createPlan,
updatePlan,
deletePlan
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
index.js:
const express = require('express');
// const methodOverride = require('method-override');
const https = require('https');
const fs = require('fs');
const bodyParser = require('body-parser');
const app = express();
const db = require('./queries');
const port = 3000;
const cors = require('cors');
// var server = express();
var urlencodedParser = bodyParser.urlencoded({ extended: true });
// override with POST having ?_method=DELETE
// app.use(methodOverride('_method'));
app.use(cors());
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true
})
);
app.options('*', cors()); // include before other routes
app.get('/', (request, response) => {
response.json({ info: 'Node.js, Express, and Postgres API' });
});
app.get('/plans', db.getPlans);
//app.get("/plans/:id", db.getPlanById);
app.post('/plans', urlencodedParser, db.createPlan);
app.post('/testpost', urlencodedParser, function(req, res) {
console.log(req.body);
});
app.put('/plans/:id', db.updatePlan);
app.delete('/plans/:planName', urlencodedParser, db.deletePlan);
// console.log(req.body);
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
我的服务器返回以下内容:
Example app listening on port 3000!{}
Result {
command: 'DELETE',
rowCount: 0,
oid: null,
rows: [],
fields: [],
_parsers: [],
_types: TypeOverrides {
_types: {
getTypeParser: [Function: getTypeParser],
setTypeParser: [Function: setTypeParser],
arrayParser: [Object]
},
text: {},
binary: {}
},
RowCtor: null,
rowAsArray: false
} {
id: '5',
plan_name: 'hello world',
plan_id: 'ssss'
}
Result {
command: 'DELETE',
rowCount: 0,
oid: null,
rows: [],
fields: [],
_parsers: [],
_types: TypeOverrides {
_types: {
getTypeParser: [Function: getTypeParser],
setTypeParser: [Function: setTypeParser],
arrayParser: [Object]
},
text: {},
binary: {}
},
RowCtor: null,
rowAsArray: false
}
如果我对Postman尝试相同,则会得到:
“已删除ID为[object Object]的计划”
在此屏幕快照中,在应用程序中完成操作后,请求在屏幕上抛出的错误。
这是前端的JS中的内容:
// ************ Delete User ************
function deletePlan(event) {
event.preventDefault();
// Pop up a confirmation dialog
let confirmation = confirm('Are you sure you want to delete this plan?');
// Check and make sure the user confirmed
if (confirmation === true) {
// If they did, do our delete
$.ajax({
type: 'DELETE',
url: 'http://blusrv019.blue.works:3000/plans/' + planName
}).done(function(response) {
// Check for a successful (blank) response
if (response.msg === '') {} else {
alert('Error: ' + response.msg);
}
});
} else {
// If they said no to the confirm, do nothing
return false;
}
}
// ********* Delete User btn click *********
$('#deletePlanBtn').on('click', deletePlan);
我觉得我错过了一些非常愚蠢的东西,但是还没弄清楚。
谁能提供一些智慧?我整天都在搜索WWW,但没有找到类似的情况或解决方案,并且以多种方式更改了我的代码,以至于我迷失了方向。
任何建议都值得赞赏。如果我的帖子不合适或结构正确(我仍然是新手),也感到抱歉。
答案 0 :(得分:1)
您正在寻找planName
作为queries.js
文件正文的一部分,但是在index.js
和前端,您正在传递planName
作为网址参数:
url: 'http://blusrv019.blue.works:3000/plans/' + planName
您应该在AJAX请求的正文中提供planName
或将queries.js
文件更改为使用request.params.planName
。