gpt4 book ai didi

json - 如何将 JSON 从服务器发送到 EJS 文件并解析它?

转载 作者:太空宇宙 更新时间:2023-11-04 02:37:57 26 4
gpt4 key购买 nike

我在 Node.js/Express 应用程序中添加了一个 Web 表单,我正在尝试为其设置验证和清理,但遇到了解析问题。我使用 Express-Validator 中间件和 Node-Validator 模块进行验证和清理。到目前为止,看起来我正确安装了它们,并且我还能够输出我想要在模板上解析的 JSON 的字符串化版本。

这是我的代码:

Server.js

var express          = require ('express'), 
organization = require('./routes/organizations'),
expressValidator = require('express-validator'),
check = require('validator').check,
sanitize = require('validator').sanitize,
Validator = require('validator').Validator;

var app = express();

app.configure(function () {
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use('/css', express.static(__dirname + '/public/css'));
app.use('/js', express.static(__dirname + '/public/js'));
app.use('/views', express.static(__dirname + '/public/views'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'keyboard cat' }));
app.set('view engine', 'ejs');
app.use(expressValidator());
app.use(app.router);
});


app.set('views', __dirname + '/public/views');

app.get('/', function(req, res){
res.render('index');
});


app.get('/submit-organization', function(req, res){
res.render('submit-organization', {
title: 'Form Validation Example is currently Valid',
message: '',
errors: ''
});
});

//Post
app.post('/submit-organization', function(req,res){

/**
req.onValidationError(function (msg) {
var errors = req.validationErrors();
var mappedErrors = req.validationErrors(true);
console.log("errors are: " + mappedErrors);
res.render('submit-organization',{errors: msg});
});

req.check('name', 'Name is required').notEmpty();
req.check('description', 'A description is required').notEmpty();
**/

req.assert('name', 'Name is required').notEmpty();
req.assert('description', 'A description is required').notEmpty();

var errors = req.validationErrors();
var mappedErrors = req.validationErrors(true);

//console.log(mappedErrors);
res.render('submit-organization',{errors: mappedErrors});





});

app.listen(3002);
console.log('Listening on port 3002...');

submit-organization.js

<% include header %>
<div class="pure-u-1">
<div style="color:red">
<%- JSON.stringify(errors.name) %>
</div>
<form action="/submit-organization" method="post" class="pure-form pure-form-stacked">
<fieldset>
<legend>Submit Organization</legend>

<p>An organization can be anything from a club to that running group you might be in</p>

<div class="control-group">
<label for="name">Name</label>
<input id="name" type="text" name="name" placeholder="My Organizaton Name">
</div>

<div class="control-group">
<label for="category">Category</label>
<select id="category" name="category">
<option>category 1</option>
<option>category 2</option>
<option>category 3</option>
</select>
</div>

<div class="control-group">
<label for="keywords">Keywords</label>
<input id="keywords" type="text" name="keywords" placeholder="running, excerise, beer, blah">
</div>

<div class="control-group">
<label for="website">Website</label>
<input id="website" type="text" name="website" placeholder="www.example.com">
</div>

<div class="control-group">
<label for="description">Description</label>
<textarea id="description" name="description" style="width:400px;height:100px;"></textarea>
</div>

<div id="meeting-times">

<h4>When</h4>


<div class="inline-block">
<div class="day"><label>Monday</label></div>
<label>@</label>
<input id="monday-venue" type="text" name="monday_venue" placeholder="Some Place">
<input id="monday-starttime" class="time" type="text" name="monday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="monday-endtime" class="time" type="text" name="monday_endtime" placeholder="9:00 AM">
</div>

<div class="inline-block">
<div class="day"><label>Tuesday</label></div>
<label>@</label>
<input id="tuesday-venue" type="text" name="tuesday_venue" placeholder="Some Place">
<input id="tuesday-starttime" class="time" type="text" name="tuesday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="tuesday-endtime" class="time" type="text" name="tuesday_endtime" placeholder="9:00 AM">
</div>

<div class="inline-block">
<div class="day"><label>Wednesday</label></div>
<label>@</label>
<input id="wednesday-venue" type="text" name="wednesday_venue" placeholder="Some Place">
<input id="wednesday-starttime" class="time" type="text" name="wednesday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="wednesday-endtime" class="time" type="text" name="wednesday_endtime" placeholder="9:00 AM">
</div>

<div class="inline-block">
<div class="day"><label>Thursday</label></div>
<label>@</label>
<input id="thursday-venue" type="text" name="thursday_venue" placeholder="Some Place">
<input id="thursday-starttime" class="time" type="text" name="thursday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="thursday-endtime" class="time" type="text" name="thursday_endtime" placeholder="9:00 AM">
</div>

<div class="inline-block">
<div class="day"><label>Friday</label></div>
<label>@</label>
<input id="friday-venue" type="text" name="friday_venue" placeholder="Some Place">
<input id="friday-starttime" class="time" type="text" name="friday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="friday-endtime" class="time" type="text" name="friday_endtime" placeholder="9:00 AM">
</div>


<div class="inline-block">
<div class="day"><label>Saturday</label></div>
<label>@</label>
<input id="saturday-venue" type="text" name="saturday_venue" placeholder="Some Place">
<input id="saturday-starttime" class="time" type="text" name="saturday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="saturday-endtime" class="time" type="text" name="saturday_endtime" placeholder="9:00 AM">
</div>

<div class="inline-block">
<div class="day"><label>Sunday</label></div>
<label>@</label>
<input id="sunday-venue" type="text" name="sunday_venue" placeholder="Some Place">
<input id="sunday-starttime" class="time" type="text" name="sunday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="sunday-endtime" class="time"type="text" name="sunday_endtime" placeholder="9:00 AM">
</div>

</div>

<button type="submit" class="pure-button pure-button-primary">Post</button>

</fieldset>
</form>
</div>
<% include footer %>

*在上面的文件中,我获取了如下错误之一的 JSON:*

<%- JSON.stringify(errors.name) %>

它输出验证错误之一的 JSON,如下所示:

{"param":"name","msg":"Name is required","value":""}

但是,我只想在模板上显示“msg”对象,但是当我使用如下代码时,我收到了 TypeError:

<%- JSON.stringify(errors.name.msg) %>

请告诉我这些信息是否足够,或者你们是否需要有关我在这里要完成的任务的更多详细信息。

  • 史蒂夫

最佳答案

@robertklep,

正在填充。感谢您的帮助,但我终于弄清楚了。我认为这一切都是错误的,因为我认为我可以在 EJS 模板中编写代码来循环遍历映射的错误。我没有编写这段代码(我认为这是不可能的,因为我尝试了几乎所有我能想到的方法),而是在 server.js 文件中为每个验证错误创建了一个变量,然后通过 JSON 将它们传递到我的 EJS 模板。如果有人感兴趣的话,这是我的最终代码:

server.js

var express          = require ('express'),
dbConnection = require('./models/dbConnection'),
organization = require('./routes/organizations'),
home = require('./routes/home'),
expressValidator = require('express-validator'),
check = require('validator').check,
sanitize = require('validator').sanitize,
Validator = require('validator').Validator;

var app = express();

app.configure(function () {
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use('/css', express.static(__dirname + '/public/css'));
app.use('/js', express.static(__dirname + '/public/js'));
app.use('/views', express.static(__dirname + '/public/views'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'keyboard cat' }));
app.set('view engine', 'ejs');
app.use(expressValidator());
app.use(app.router);
});


app.set('views', __dirname + '/public/views');


app.get('/',home.getOrganizations);


app.get('/submit-organization', function(req, res){
res.render('submit-organization', {
title: 'Form Validation Example is currently Valid',
message: '',
errors: ''
});
});

//Post
app.post('/submit-organization', function(req,res){

req.assert('name', 'Name is required').notEmpty();
req.assert('description', 'A description is required').notEmpty();


var errors = req.validationErrors();
var mappedErrors = req.validationErrors(true);

var myerror1 = mappedErrors.name.msg;
var myerror2 = mappedErrors.description.msg;
res.render('submit-organization',{errors: {error1: myerror1, error2: myerror2}});

});

app.listen(3002);
console.log('Listening on port 3002...');

submit-organizations.ejs(抱歉,我在原来的问题中错误地写了“submit-organizations.js”):

<% include header %>
<div class="pure-u-1">
<div style="color:red">
<ul>
<li><%- JSON.stringify(errors.error1) %></li>
<li><%- JSON.stringify(errors.error2) %></li>
</ul>
</div>
<form action="/submit-organization" method="post" class="pure-form pure-form-stacked">
<fieldset>
<legend>Submit Organization</legend>

<p>An organization can be anything from a club to that running group you might be in</p>

<div class="control-group">
<label for="name">Name</label>
<input id="name" type="text" name="name" placeholder="My Organizaton Name">
</div>

<div class="control-group">
<label for="category">Category</label>
<select id="category" name="category">
<option>category 1</option>
<option>category 2</option>
<option>category 3</option>
</select>
</div>

<div class="control-group">
<label for="keywords">Keywords</label>
<input id="keywords" type="text" name="keywords" placeholder="running, excerise, beer, blah">
</div>

<div class="control-group">
<label for="website">Website</label>
<input id="website" type="text" name="website" placeholder="www.example.com">
</div>

<div class="control-group">
<label for="description">Description</label>
<textarea id="description" name="description" style="width:400px;height:100px;"></textarea>
</div>

<div id="meeting-times">

<h4>When</h4>


<div class="inline-block">
<div class="day"><label>Monday</label></div>
<label>@</label>
<input id="monday-venue" type="text" name="monday_venue" placeholder="Some Place">
<input id="monday-starttime" class="time" type="text" name="monday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="monday-endtime" class="time" type="text" name="monday_endtime" placeholder="9:00 AM">
</div>

<div class="inline-block">
<div class="day"><label>Tuesday</label></div>
<label>@</label>
<input id="tuesday-venue" type="text" name="tuesday_venue" placeholder="Some Place">
<input id="tuesday-starttime" class="time" type="text" name="tuesday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="tuesday-endtime" class="time" type="text" name="tuesday_endtime" placeholder="9:00 AM">
</div>

<div class="inline-block">
<div class="day"><label>Wednesday</label></div>
<label>@</label>
<input id="wednesday-venue" type="text" name="wednesday_venue" placeholder="Some Place">
<input id="wednesday-starttime" class="time" type="text" name="wednesday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="wednesday-endtime" class="time" type="text" name="wednesday_endtime" placeholder="9:00 AM">
</div>

<div class="inline-block">
<div class="day"><label>Thursday</label></div>
<label>@</label>
<input id="thursday-venue" type="text" name="thursday_venue" placeholder="Some Place">
<input id="thursday-starttime" class="time" type="text" name="thursday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="thursday-endtime" class="time" type="text" name="thursday_endtime" placeholder="9:00 AM">
</div>

<div class="inline-block">
<div class="day"><label>Friday</label></div>
<label>@</label>
<input id="friday-venue" type="text" name="friday_venue" placeholder="Some Place">
<input id="friday-starttime" class="time" type="text" name="friday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="friday-endtime" class="time" type="text" name="friday_endtime" placeholder="9:00 AM">
</div>


<div class="inline-block">
<div class="day"><label>Saturday</label></div>
<label>@</label>
<input id="saturday-venue" type="text" name="saturday_venue" placeholder="Some Place">
<input id="saturday-starttime" class="time" type="text" name="saturday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="saturday-endtime" class="time" type="text" name="saturday_endtime" placeholder="9:00 AM">
</div>

<div class="inline-block">
<div class="day"><label>Sunday</label></div>
<label>@</label>
<input id="sunday-venue" type="text" name="sunday_venue" placeholder="Some Place">
<input id="sunday-starttime" class="time" type="text" name="sunday_starttime" placeholder="8:00 AM"> <label>-</label>
<input id="sunday-endtime" class="time"type="text" name="sunday_endtime" placeholder="9:00 AM">
</div>

</div>

<button type="submit" class="pure-button pure-button-primary">Post</button>

</fieldset>
</form>
</div>
<% include footer %>

关于json - 如何将 JSON 从服务器发送到 EJS 文件并解析它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20290422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com