试图从localhost:3000 / todos /在mongodb中创建数据后尝试获取数据,并且在节点[UnhandledPromiseRejectionWarning]中收到错误:错误[ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头]以下
// Require Express
const express = require("express");
// Setting Express Routes
const router = express.Router();
// Set Up Models
const Todo = require("../models/todo");
// Get All Todos
router.get("/", async (req, res) => {
try {
const todo = await Todo.find();
res.json(todo);
} catch (err) {
res.json({ message: err });
}
});
这是我的app.js,具有到localhost:3000 / todos /
的必要路由// Require Express
const express = require("express");
const app = express();
// Require Body-Parser
const bodyParser = require("body-parser");
// Require Cors
const cors = require("cors");
// Middlewares - [Set bodyParser before calling routes]
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Import Routes
const todoRoutes = require("./routes/todo");
// Setup Routes Middlewares
app.use("/", todoRoutes);
app.use("/todos", todoRoutes);
我想看到我在localhost:3000 / todos /上的输出数据,但我在localhost:3000 /上得到了它。谢谢
答案 0 :(得分:0)
好吧,我不知所措,这是我的路线导致的,我只是调整了路线的指向。
// Import Routes
const todoRoutes = require("./routes/todo");
// Setup Routes Middlewares
app.use("/todos", todoRoutes);