NodeJs中错误的重定向



我创建了一个关于待办事项列表的nodeJs项目但我的删除锚遇到了一个问题我的重定向有问题,它不工作我尝试了多种方法,但当我点击它时,它仍然把我发送到id URL,这不是我想要的。

my ejs code

`    <button class="Clear" data-doc="<% lists._id %>">Clear List</button>

<div class="Assignments">
<ul class="AssignmentsList">
<h1><% if(lists.length>0){ %></h1>
<h1>My lists</h1>
<% lists.forEach(element =>{ %>
<h1><%= element.name %></h1>
<h1> <%= element._id  %></h1>
<a  href="/list<%= element._id  %>" class="deletee" data-myvalue="<%= element._id %>">Delete</a>
<% }) %>
<% } %>
</ul>

</div>
<script >

let trashcan = document.querySelector(".Clear");
trashcan.addEventListener('click',(e)=>{
console.log("endpoint");
let endpoint = `/list/${trashcan.dataset.doc}`;
fetch(endpoint,{method:'delete'})
.then(response => response.json())
.then(data => window.location.href= data.redirect)
.catch(err => console.log(err));
})

let owntrash = document.querySelector('.deletee');
owntrash.addEventListener('click',(e)=>{
console.log("fff");
let endpoint = `/list/${owntrash.dataset.myvalue}`;
console.log(endpoint);
fetch(endpoint,{method:'delete'})
.then(response => response.json())
.then(data =>  window.location.href= data.redirect)
.catch(err => console.log(err));
})
</script>
App code 
app.delete('/list',(req,res)=>{
const id = req.params.id;
console.log(id);
if(id != undefined)
{
console.log("single");
}
else{
console.log("gg");
myList.find()
.then(result => result.forEach(element => {
myList.findByIdAndDelete(element._id)
.catch(err=>console.log(err));
}
))
.then(result => res.json({redirect: '/'}))
.catch(err=>console.log(err));
}
})
app.delete('/list/:id',(req,res)=>{
const id = req.params.id;
console.log(id);
myList.findByIdAndDelete(id)
.then(resualt => res.json({redirect: '/list'}))
.catch(err=>console.log(err));
})

app.get('/list',(req,res)=>{
myList.find()
.then(result => res.render('list',{lists:result}))
.catch(err=>console.log(err));
})
`

删除todo后将我重定向到同一页面(如刷新)


app.delete('/list/:id',(req,res, next)=>{
const id = req.params.id;
console.log(id);
myList.findByIdAndDelete(id)
.then(results => next)
.catch(err=>console.log(err));
}, (req, res, next) => {
//then write the code of what you want to receive after delete
// to get all the remaining you do this
myList.find()
.then(result => res.render('list',{lists:result}))
.catch(err=>console.log(err));
})

相关内容

  • 没有找到相关文章

最新更新