Javascript Express获取URL并删除特定对象
现在我正在用Express和LowDB开发CRUD(创建、读取、更新、删除)应用程序 我成功地开发了创建和读取,但删除功能不起作用 有这样的详细页面 正如你所看到的,有垃圾桶图标,实际上我也有一些问题提交按钮,但这并不重要,所以我只是提交按钮 这是哈巴狗档案的一部分Javascript Express获取URL并删除特定对象,javascript,node.js,express,pug,Javascript,Node.js,Express,Pug,现在我正在用Express和LowDB开发CRUD(创建、读取、更新、删除)应用程序 我成功地开发了创建和读取,但删除功能不起作用 有这样的详细页面 正如你所看到的,有垃圾桶图标,实际上我也有一些问题提交按钮,但这并不重要,所以我只是提交按钮 这是哈巴狗档案的一部分 aside#Delete.is-visible form(method="post" action='/delete_process') input(type='submit') a#DeleteButton(rol
aside#Delete.is-visible
form(method="post" action='/delete_process')
input(type='submit')
a#DeleteButton(role='button')
img(src='https://png.icons8.com/ios/35/000000/waste-filled.png' id="go_up")
span.text
| Delete
br
b this
详细信息页面有URL(例如:DIR1WTqr2)和DB id
删除过程代码是这样的
/* Delete Process */
router.post('/delete_process', function (req, res) {
// GET Delete ID from URL
let id=req.params.id;
console.log(id);
db.get('project').find({
id: id
}).remove().write();
});
/* GET Detail View Page */
router.get('/:id', (req, res, next) => {
// GET URL params and put it into :id
let id = req.params.id;
// console.log(id);
// GET data id to use Object
let data = db.get('project').find({
id: id
}).value();
// Get github URL
let url = data.githuburl;
// Use Request Module to parsing Web page
request(url, function (error, response, html) {
if (error) {
throw error
};
// Parsing readme ID in github page
var $ = cheerio.load(html);
$('#readme').each(function () {
// save to readme Variable
let readme = $(this).html();
// console.log(data);
// console.log(id);
res.render('detail', {
dataarray: data,
name: data.name,
url: data.url,
explanation: data.explanation,
imgurl: data.imgurl,
markdown: readme,
startDate: data.pjdate1,
endDate: data.pjdate2,
githuburl: data.githuburl,
sumlang: data.sumlang
});
});
});
});
我想我可以获取URL并在json文件中找到id为的数据。但它不起作用。当我试图控制台req.params.id时,它说未定义
我认为视图细节页面会发生冲突,因为细节页面代码是这样的
/* Delete Process */
router.post('/delete_process', function (req, res) {
// GET Delete ID from URL
let id=req.params.id;
console.log(id);
db.get('project').find({
id: id
}).remove().write();
});
/* GET Detail View Page */
router.get('/:id', (req, res, next) => {
// GET URL params and put it into :id
let id = req.params.id;
// console.log(id);
// GET data id to use Object
let data = db.get('project').find({
id: id
}).value();
// Get github URL
let url = data.githuburl;
// Use Request Module to parsing Web page
request(url, function (error, response, html) {
if (error) {
throw error
};
// Parsing readme ID in github page
var $ = cheerio.load(html);
$('#readme').each(function () {
// save to readme Variable
let readme = $(this).html();
// console.log(data);
// console.log(id);
res.render('detail', {
dataarray: data,
name: data.name,
url: data.url,
explanation: data.explanation,
imgurl: data.imgurl,
markdown: readme,
startDate: data.pjdate1,
endDate: data.pjdate2,
githuburl: data.githuburl,
sumlang: data.sumlang
});
});
});
});
因为
:id
当我单击/delete\u过程时,它会进入详细页面。我想帕格对缩进非常具体。表单中没有包含您的按钮-它是一个兄弟。当您希望一个元素包含在另一个元素中时,请确保始终有两个空格
将pug模板更改为此,按钮将正确提交:
aside#Delete.is-visible
form(method="post" action='/delete_process')
input(type='submit')
a#DeleteButton(role='button')
img(src='https://png.icons8.com/ios/35/000000/waste-filled.png' id="go_up")
span.text
| Delete
br
b this
但是等等,还有更多!你没有把餐馆的身份证和表格一起递回去
我看不出你是如何在帕格中使用餐厅id的,但你也需要在表单中添加它。让我们假设它是帕格模板中名为restaurant.id
的变量
您的node.js route处理程序正在尝试读取req.params.id
,这意味着它将查找url以获取餐厅的id。您可以将id添加到请求中,如下所示:
aside#Delete.is-visible
form(method="post" action='/delete_process/' + restaurant.id)
input(type='submit')
aside#Delete.is-visible
form(method="post" action='/delete_process/' + restaurant.id)
input(type='hidden' name='id' value= restaurant.id)
input(type='submit')
router.post('/delete_process', function (req, res) {
// GET Delete ID from form field
let id= req.body.id;
然后,您还需要修改路由以包含id参数:
router.post('/delete_process/:id', function (req, res) {
另一种方法是添加一个隐藏的表单字段,该字段随表单一起发送id(这解释了这个概念)。如果这样做,可以将表单更改为如下所示:
aside#Delete.is-visible
form(method="post" action='/delete_process/' + restaurant.id)
input(type='submit')
aside#Delete.is-visible
form(method="post" action='/delete_process/' + restaurant.id)
input(type='hidden' name='id' value= restaurant.id)
input(type='submit')
router.post('/delete_process', function (req, res) {
// GET Delete ID from form field
let id= req.body.id;
然后,您的删除处理路径可以像这样拾取它:
aside#Delete.is-visible
form(method="post" action='/delete_process/' + restaurant.id)
input(type='submit')
aside#Delete.is-visible
form(method="post" action='/delete_process/' + restaurant.id)
input(type='hidden' name='id' value= restaurant.id)
input(type='submit')
router.post('/delete_process', function (req, res) {
// GET Delete ID from form field
let id= req.body.id;
传递id的第一种方法(在url中)是ajax请求的标准方法,第二种方法(作为表单中的字段)是发布表单的标准方法