Stack Overflow Asked by Liad Goren on December 18, 2021
I must be missing something majorly important here
router.get("/", async (req, res, next) => {
let posts = await loadPostsCollection();
console.log(await posts.find({}).toArray());
res.send(await posts.find().toArray());
});
async function loadPostsCollection() {
const client = await mongodb.MongoClient.connect(uri, {
// useNewUrlParser: true,
useUnifiedTopology: true,
});
return client.db("stack_1").collection("posts");
}
why do i get headers already sent error?
Express js @latest
MongoDb Atlas free cluster
db with single item in a ‘posts’ collection
I must be missing something incredibly simple.
The console log works as intended…
I’m pretty sure all my async await calls are at the right place…
Help..?
EDIT : ERROR:
http://localhost:3000/api/posts: 2020-07-23T23:57:06+03:00
[ { _id: 5f19e338adcae71eca1c3658, name: 'liad', status: 'active' } ]
(node:7894) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:536:11)
at ServerResponse.header (/media/liad/Data/liad_dev/vue-express-mongo/node_modules/express/lib/response.js:771:10)
at ServerResponse.send (/media/liad/Data/liad_dev/vue-express-mongo/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/media/liad/Data/liad_dev/vue-express-mongo/node_modules/express/lib/response.js:267:15)
at ServerResponse.send (/media/liad/Data/liad_dev/vue-express-mongo/node_modules/express/lib/response.js:158:21)
at /media/liad/Data/liad_dev/vue-express-mongo/server/routes/api/posts.js:10:7
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:7894) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:7894) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Edit 2:
refactored everything to use mongoose
I get the same error !
const post = new Post({
title: req.body.title,
desc: req.body.desc,
});
console.log(post);
post
.save()
.then((response) => {
res.json(response);
})
.catch((err) => {
res.json({ message: err });
});
});
I've found a solution.
The way I've defined my application is trying to dynamically access my routers without having to define them one by one
let routerPath = "/api/members";
app.use((req, res, next) => {
let routerPath = req.path;
app.use(`${routerPath}`, require(`./routes${routerPath}`));
next();
});
My error was caused because I've had an extra next();
let routerPath = "/api/members";
app.use((req, res, next) => {
let routerPath = req.path;
next();
app.use(`${routerPath}`, require(`./routes${routerPath}`));
next();
});
For some reason that wrecked my app...
Thanks for trying to help everyone
Answered by Liad Goren on December 18, 2021
in the second occurence of posts.find there is no empty object as argument. Could that be an issue? Also it would be safer to put the function bodies of async functions in a try/catch block as exceptions are not catched right now.
Answered by Jeroen van Aert on December 18, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP