Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
How to get access to variable from post into get method with mongoDB

i created variable in app.post, and i want to use it in app.get, but before this i added this one to my database, but i don't know how to get access to this variable in other function, here is my code

const express = require("express");
const { default: mongoose } = require("mongoose");
const app = express();
const port = 3000;
const Task = require(`./models/task`);
require("./db/moongose");

// sendFile will go here
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(express.urlencoded({ extended: false }));

app.get("/task", (req, res) => {
  res.render("../public/mainPage.ejs", {tasks: mongoose});
});

app.post("/task", async (req, res) => {
  const task = new Task({
    description: req.body.task,
  });

  try {
    await task.save();


    console.log(task.description);
    res.redirect("/task");
  } catch (e) {}
});

app.listen(port);
console.log("Server started at http://localhost:" + port);

and all i want to do is get acces to task.description in app.get

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!