0

personalinfo.js

var express = require("express"),
router = express.Router(),
personalinfo = require("../models/personalinfo");
// To show PersonalInfo 
router.get("/new", function(req, res) {
res.render("personalinfo/new");
});
// To create personalInfo date
router.post("/new", function(req, res) {
const personalInfo = req.body.personalinfo;
console.log(personalInfo);
personalinfo.create(req.body.personalinfo, function(err, ninfo) {
    if (err) {
        res.send(err);
    } else {
        res.redirect("/objective/new");
    }
});
});
module.exports = router;

obj.js

var express = require("express"),
router = express.Router(),
objective = require("../models/objective");

router.get("/new", function(req, res) {
res.render("objective/new");
});

router.post("/new", function(req, res) {
objective.create(req.body.objective, function(err, nobj) {
    if (err) {
        res.send(err);
    } else {
        res.send(nobj);
    }
});
});

module.exports = router;

From the personalinfo.js route file I want to use the personalInfo variable in obj.js route file. How can I do that. Thanks for helping me. :)

Sathis
  • 1
  • Please don't name two variables `personalinfo` and `personalInfo` that different only in a very hard to see way. That's just a bad practice. I just wasted some time working on an answer to your question until I realized you had two separate variables that look nearly identical. It just makes your code confusing and hard to read. – jfriend00 Jul 06 '20 at 06:42
  • And, NO, you can't directly share a variable created in one route with another route. Your server is a multi-user server that many different users can use. So, you can't store data for ONE user into a variable and expect it to still be there on some future route. Instead, you would typically use something like `express-session` and store the state for that user in their session object which you can then access from a future route. – jfriend00 Jul 06 '20 at 06:44
  • Also, you should be very, very careful about what state you store for a user. Web servers are simpler and more scalable when they are as stateless as possible. In general, you want to keep state in the client or the URL (for transient things) or for more permanent data in a data store, not in your server's memory. – jfriend00 Jul 06 '20 at 06:45
  • To answer your question about the best overall architecture for moving some data from one route to another, we'd need to know what you're really trying to do in these two routes. If it's just some piece of temporal state that is needed to render the next page, then it may be best to just make it a query parameter on the redirect URL where the next route can access it from the URL. That's entirely stateless and simple. – jfriend00 Jul 06 '20 at 06:48

1 Answers1

0

You can use a module in node.

npm install connect-flash
var express = require('express');
var flash = require('connect-flash');
var app = express();
app.use(flash());


app.get('/login', function(req, res){
  req.flash('profileInfo', 'SomeText')
});

app.get('/profile', function(req, res){

  let message = req.flash('profileInfo')
  res.render('index', { message: message });
});

More info about NPM package

xMayank
  • 1,875
  • 2
  • 5
  • 19