我是这里编程的初学者,我正在尝试通过/admin.ejs将/或系统提交给/home.ejs。我将数据粘贴到管理页面上并且它正在工作但是当我尝试渲染到主页时它说数据没有定义:/`谢谢你提前。
paths
-model
--input.js
-routes
--adminRouter.js
--indexRouter.js
-views
--admin
---admin.ejs
--partials
---header.ejs
---footer.ejs
--home.ejs
-index.js
adminRouter.js
可能是我的错误在这里
const express = require("express"),
session = require('express-session'),
bodyParser = require("body-parser"),
app = express(),
router = express.Router();
/////yancilar//////
var jsonParser = bodyParser.json();
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.set("view engine","ejs" );
///////////////
router.get('/admin', (req, res)=>{
res.render('admin/admin');
});
router.post('/admin',urlencodedParser, (req, res)=>{
console.log(req.body);
const data = req.body.input ;
res.render('home', {data : data});
console.log(data);
});
home.ejs
<% include ./partials/header %>
<div class="container">
<h1 class="my-4 text-center text-lg-left">Thumbnail Gallery</h1>
<div class="row text-center text-lg-left">
<div class="col-lg-3 col-md-4 col-xs-6">
<a href='' class="d-block mb-4 h-100 ">
<% if (data) { %>
<h2><%- data %></h2>
<% } %>
<img class="img-fluid img-thumbnail" src="" alt="">
</a>
</div>
</div>
</div>
<% include ./partials/footer %>
index.js
const mongoose = require("mongoose"),
express = require("express"),
app = express(),
input = require("./models/input"),
bodyParser = require("body-parser"),
passport = require("passport"),
expressSession = require("express-session"),
LocalStrategy = require("passport-local");
///pass////
app.use(passport.initialize());
app.use(passport.session());
//passport.use(new LocalStrategy(input.authenticate()));
//passport.serializeUser(input.serializeUser());
//passport.deserializeUser(input.deserializeUser());
////Routes///////////////////
const indexRouter = require("./routes/indexRouter");
const adminRouter = require("./routes/adminRouter");
///App Config////////////////
app.set("view engine","ejs" );
app.use(express.static("public"));
mongoose.connect('mongodb://localhost/game');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
/////routers use ////////////
app.use(adminRouter);
app.use(indexRouter);
admin.ejs
<% include ../partials/header %>
<form action="admin" method="POST" >
<label class="sr-only" for="input">Name</label>
<input type="text" class="form-control mb-2 mr-sm-2 mb-sm-0" id="input" placeholder="Jane Doe" name="input" />
<label class="sr-only" for="inlineFormInputGroup">Username</label>
<div class="form-check mb-2 mr-sm-2 mb-sm-0">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<div>
</div>
<% include ../partials/footer %>
indexRouter
const express = require("express"),
router = express.Router(),
app = express();
router.get('/', function(req, res) {
res.render('home');
});
module.exports = router;
答案 0 :(得分:0)
仅仅因为POST
数据/admin
并使用该数据呈现主页,并不意味着导航到/home
时数据会一直存在。
在这里查看您的路线。没有数据传递回家。
router.get('/', function(req, res) {
res.render('home');
});
您可以使用数据库,mongoose是一个常见的数据库:
router.post('/admin', urlencodedParser, (req, res) => {
const data = req.body.input ;
// instead of calling res.render();
// store it in a database here
});
然后您可以检索数据并将其传递给任何路线。
router.get('/', function(req, res) {
// query the database here
// const data = database.query();
res.render('home', {data: data} );
});