我正在构建一个Netflix克隆,但是在react中使用useffect钩子时,我的后端API出现了一个问题。我收到了404错误(未找到)。我已经在package.json文件中添加了我的路由作为代理。这是一个get请求,用于获取我放入MongoDB数据库中的电影列表。当我使用postman时,我确实会获取该列表。don'我不明白为什么我会得到这个错误。请分享任何想法。
包.json文件
"name": "new",
"version": "0.1.0",
"private": true,
"dependencies": {
"@material-ui/core": "^4.11.4",
"@material-ui/icons": "^4.11.2",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"axios": "^0.21.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"sass": "^1.35.1",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:8800/api/"
}
主页.jsx
import { useEffect, useState } from "react";
import Featured from "../../components/featured/Featured";
import List from "../../components/list/List";
import Navbar from "../../components/navbar/Navbar";
import axios from "axios";
import "./home.scss";
const Home = ({ type }) => {
// eslint-disable-next-line
const [lists, setLists] = useState([]);
// eslint-disable-next-line
const [genre, setGenre] = useState(null);
useEffect(() => {
const getRandomLists = async () => {
try {
const res = await axios.get(
`lists${type ? "?type=" + type : ""}${
genre ? "&genre=" + genre : ""
}`,
{
headers: {
token:
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYzYTA1ZjliZTRiZmEzYTBhOWMxYTQ5ZCIsImlzQWRtaW4iOnRydWUsImlhdCI6MTY3MTcxMjgyMCwiZXhwIjoxNjcyMTQ0ODIwfQ.W2e-RJ0XIQIVgGT47yhEOVOtWbdR5yYk11zmvN3vLzk",
},
}
);
console.log(res);
} catch (error) {
console.log(error);
}
};
getRandomLists();
}, [type, genre]);
return (
<div className="home">
<Navbar />
<Featured type={type} />
<List />
<List />
<List />
<List />
<List />
<List />
</div>
);
};
export default Home;
索引.js后端
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const authRoute = require("./routes/auth");
const userRoute = require("./routes/users");
const movieRoute = require("./routes/movies");
const listRoute = require("./routes/lists");
const cors = require("cors");
app.use(cors());
dotenv.config();
mongoose
.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("DB Connection Successful!"))
.catch((err) => console.log(err));
app.use(express.json());
app.use("/api/auth", authRoute);
app.use("/api/users", userRoute);
app.use("/api/movies", movieRoute);
app.use("/api/lists", listRoute);
app.listen(8800, () => {
console.log("backend server is running on port 8800");
});
后端中的list.js
router.get("/", verify, async (req, res) => {
const typeQuery = req.query.type;
const genreQuery = req.query.genre;
let list = [];
try {
if (typeQuery) {
if (genreQuery) {
list = await List.aggregate([
{ $sample: { size: 10 } },
{ $match: { type: typeQuery, genre: genreQuery } },
]);
} else {
list = await List.aggregate([
{ $sample: { size: 10 } },
{ $match: { type: typeQuery } },
]);
}
} else {
list = await List.aggregate([{ $sample: { size: 10 } }]);
}
res.status(200).json(list);
} catch (error) {
res.status(500).json(error);
}
});```
1条答案
按热度按时间5sxhfpxr1#
我想通了我的令牌已经过期的问题。我只需要回到 Postman 那里登录,从 Postman 那里得到一个新的令牌。