简体   繁体   English

当我使用 fetch my.then 代码不起作用

[英]When I use fetch my .then code isn't working

So I am trying to redirect after I am deleting the page, it get's deleted from the database but it doesn't redirect me to my homepage.因此,我在删除页面后尝试重定向,它已从数据库中删除,但不会将我重定向到我的主页。 It worked fine when I was using json-server locally, but when I started using Mongoose it wasn't working properly and wasn't redirecting.当我在本地使用 json-server 时它工作正常,但是当我开始使用 Mongoose 时它不能正常工作并且没有重定向。

The code inside .then isn't working, I tried console.log inside the .then but it didn't log .then中的代码不起作用,我在.then中尝试了 console.log 但它没有记录

I am using mongoose as my database我使用 mongoose 作为我的数据库

Here is my entire component:这是我的整个组件:

 import { useParams } from "react-router-dom"; import useFetch from "../useFetch"; import { useHistory } from "react-router-dom"; import moment from "moment"; import profile_image from "../images/picture.jpg"; const BlogDetails = () => { let blogDate = moment().format('D MMM YYYY'); const { id } = useParams(); const { data: blog, isPending, errorMsg } = useFetch("http://localhost:5000/postsdata/" + id); const history = useHistory() const handleDelete = () => { fetch('http://localhost:5000/postsdata/' + blog._id, { method: 'DELETE' }).then(() => { history.push('/'); }).catch((err) => console.log(err)) } return ( <div className="blog-details"> <div className="top-profile"> <div className="top-profile-picture"> <img src={profile_image} alt="profile-pic-top" /> </div> <div className="top-profile-name"> <p>Vishwajeet Deshmukh</p> </div> </div> {isPending && <div>Loading...</div>} {errorMsg && <div>{errorMsg}</div>} {blog && ( <article className="blog-main-content" > <div className="main-content-header"> <div className="content-title-date"> <h2 className="blogdetails-title">{blog.title}</h2> <p className="blogdetails-date">{blogDate}</p> </div> <div className="content-image"> <img src={blog.imgsrc} alt="" /> </div> </div> <div className="blogdetails-body"><p>{`${blog.postBody}`}</p></div> <button className="blogdetails-delete" onClick={handleDelete}>Delete Me</button> </article> )} </div> ); }; export default BlogDetails;

Here is my router.js which handles my delete这是我的router.js处理我的删除

 const express = require('express'); const router = express.Router(); const { Posts } = require("./models"); //<----------------------------------- CRUD OPERATIONS ------------------------------------------> router.get("/", () => { console.log("Server Connected"); }) //<---------------------------- Get Posts from Database ----------------------------> router.get("/postsdata", (req, res) => { Posts.find((err, data) => { if (err) { res.status(500).send(err); } else { res.status(201).send(data); } return null; }) }) //<------------- Get Specific Posts from Database ---------------> router.get("/postsdata/:_id", (req, res) => { const id = req.params._id; Posts.findById(id, (err, data) => { if (err) { res.status(500).send(err); throw new Error(err) } else { res.status(201).send(data); } return data; }) }) //<---------------------------- Post On the Posts Database ----------------------------> router.post("/postsdata", (req, res) => { const db = req.body; Posts.create(db, err => { if (.err) { console;log("Posted on Server"). } else { throw new Error(err) } return null }) }) //<---------------------------- Delete Posts from Database ----------------------------> router:delete("/postsdata/,id", (req. res) => { const id = req.params._id Posts,deleteOne(id, (err. data) => { if (err) { console;log(err). throw new Error(err) } else { console;log(data). } return null }) }) module;exports = router;

Hello try it with async/await sayntax你好用 async/await sayntax 试试

 const handleDelete = async () => { await fetch('http://localhost:5000/postsdata/' + blog._id, { method: 'DELETE' }); history.push('/'); }

after deleting the postdata, send a response from the API.删除 postdata 后,从 API 发送响应。


    router.delete("/postsdata/:id", (req, res) => {
    const id = req.params._id

    Posts.deleteOne(id, (err, data) => {
        if (err) {
            console.log(err);
            throw new Error(err)
        } else {
            return res.status(200).json({status: 'success'}); // try with this
        }
        return null
    })
    })

暂无
暂无

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

相关问题 当我尝试使用IFFE时,为什么这段代码不起作用? - Why isn't this code working when i try to use IFFE 当我尝试通过ID提取标签时,为什么我的jQuery无法正常工作? - why isn't my jQuery working when I try to fetch a tag by Id? 当我将其导入Wordpress网站时,为什么我的JQuery / CSS代码不起作用? - Why isn't my JQuery/CSS code working when I import it into my Wordpress website? 所以我正在尝试使用 Math.cbrt 并且我的代码没有按预期工作 - So I'm trying to use Math.cbrt and my code isn't working as expected 为什么我测试时我的Three.js代码无效? - Why my Three.js code isn't working when i test it? 帆布屋。 将代码复制到编辑器并在浏览器中运行时,为什么代码不起作用? - Canvas- house. Why isn`t the code working, when I copy it to an editor and run it in my browser? 为什么我的函数在Parse Cloud Code中不起作用? 当我在Angular和Express中测试时它可以工作 - Why isn't my function working in Parse Cloud Code? It works when I test in Angular and Express 当我重新访问页面时,我的cookie无法正常工作 - My cookie isn't working when I revisit the page 为什么我使用 css 时我的页面没有加载? - Why isn't my page loading when I use css? 当我将鼠标悬停在图像上时,我需要使用 JavaScript 更改图像的不透明度值,但我的代码不起作用 - I need to change the opacity value of an image using JavaScript when i mouse over it but my code isn't working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM