我试图建立一个有司机和乘客(拼车)的网站。 因此,当司机提供他的详细信息并点击提交时,他会转到一个URL(选择),在那里他可以选择他想要的乘客。
我写了以下代码: -
app.post('/passenger',function(req,res){
var user = new User({profile:{name:req.body.uname},
type:"passenger",
phone_no:req.body.contact,
origin:{city:req.body.pick},
destination:{city:req.body.drop}
// email:req.body.email,
// password:req.body.password
})
user.save();
res.redirect('/');
});
app.post('/driver',function(req,res){
var user = new User({profile:{name:req.body.uname},
type:"driver",
phone_no:req.body.contact,
origin:{city:req.body.pick},
destination:{city:req.body.drop}
// email:req.body.email,
// password:req.body.password
})
user.save();
console.log("heyyyy")
res.redirect('/select');
});
app.get('/select',function(req,res){
// User.find(function(err,type){
// res.render('select',{type:type});
// });
if (req.user){
console.log("U are inside")
if(req.user.type="passenger")
{
res.render('select');
}
else{
res.send("You ar not admin");
}
}
console.log("u are outside")
console.log(req.body.uname)
console.log()
});
值已保存在数据库中,但我无法检索信息(即所有乘客的列表)。
我无法处理它(即给出错误)。
请帮帮我..
答案 0 :(得分:0)
在不知道如何配置快速服务器的情况下,很难确定问题的根源,无论如何我已根据您的上下文创建了一个示例。
app.js
档案:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var engine = require('ejs-locals');
var querystring = require('querystring');
var drivers = [];
var passengers = [{name: 'Wilson'}, {name: 'Raul'}];
app.engine('ejs', engine);
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get('/', function(req, res) {
res.render('driver');
});
app.post('/driver', function(req, res) {
var driver = {
name: req.body.name,
age: req.body.age
};
drivers.push(driver);
var data = {
driver: driver,
passengers: passengers
};
res.render('passengers', data);
});
app.get('/result', function(req, res) {
var data = {
driverName: '',
passengerNames: []
};
data.driverName = req.query.driverName;
data.passengerNames = data.passengerNames.concat(req.query.passengerNames);
res.render('result', data);
});
app.post('/api/passengers', function(req, res) {
var data = {
passengerNames: req.body.passengerNames,
driverName: req.body.driverName
};
var qs = querystring.stringify(data);
res.redirect('/result?' + qs);
});
app.listen(4040, function() {
console.log('server up and running at port 4040');
});
views/layout.ejs
档案:
<!doctype html>
<html>
<head>
<title>Drivers App</title>
</head>
<body>
<div id="app">
<%- body %>
</div>
<script>
document.addEventListener('DOMContentLoaded', function(e) {
var btnNext = document.getElementById('btnNext');
btnNext.onclick = function(e) {
var allPassengers = document.querySelectorAll('input[type=checkbox]');
var selectedPassengers = [].filter.call(allPassengers, function(p) {
return p.checked;
});
var passengerNames = [].map.call(selectedPassengers, function(p) {
return p.value;
});
var driverName = document.getElementById('driverName').innerHTML;
var r = new XMLHttpRequest();
r.open('POST', '/api/passengers');
r.setRequestHeader('Content-type', 'application/json;charset=UTF-8');
r.onreadystatechange = function() {
if (r.readyState != 4 || r.status != 200) return;
document.getElementById('app').innerHTML = r.responseText;
};
r.send(JSON.stringify({
passengerNames: passengerNames,
driverName: driverName
}));
};
});
</script>
</body>
</html>
views/driver.ejs
档案:
<% layout('layout.ejs') -%>
<h1> Driver </h1>
<form method="POST" action="/driver">
<label>Name:</label>
<input id="name" name="name" type="text"/>
<br />
<label>Age:</label>
<input id="age" name="age" type="text"/>
<br />
<br />
<input type="submit" value="Submit" />
</form>
`views/passengers.ejs`
<% layout('layout') -%>
<h1> Driver </h1>
<p>Name: <span id="driverName"><%= driver.name%></span></p>
<h1> Passengers </h1>
<% passengers.forEach(function(p) {%>
<input type="checkbox" value=<%= p.name %> /> <%= p.name %> <br />
<% }); %>
<br />
<button id="btnNext">Next</button>
views/result.ejs
档案:
<h1>Success!</h1>
<h2>Driver</h2>
<h3><%= driverName %></h3>
<br />
<h2>Selected Passengers</h2>
<% passengerNames.forEach(function(name) { %>
<h3><%= name%></h3>
<% }); %>