0

在我的应用程序中,我使用 node.js 中的表单模块(https://github.com/caolan/forms)。在启动页面中显示表单时,jade 文件出现错误。任何人都可以帮助解决这个问题。

应用程序.js

  var http = require('http'),
  express = require('express'),
  app = express();

  var fields = forms.fields,
  validators = forms.validators,
  widgets = forms.widgets;

  var reg_form = forms.create({
  username: fields.string({required: true}),
  password: fields.password({required: true}),
  confirm: fields.password({
    required: true,
    validators: [validators.matchField('password')]
}),
email: fields.email()
  });

app.configure(function () {
  app.set('views', __dirname);
  app.set('view engine', 'jade');

  app.use(express.bodyParser());
  app.use(app.router);


  });

  app.get('/', function (req, res) {
     res.render('page', {
     locals: {
         title: 'Filling out the form...',
         form: reg_form.toHTML()
     }
  });
   });

 app.post('/', function (req, res) {
 reg_form.handle(req, {
    success: function (form) {
        res.render('page', {
            locals: {
                title: 'Success!'
            }
        });
    },
    other: function (form) {
        res.render('page', {
            locals: {
                title: 'Failed!',
                form: form.toHTML()
            }
        });
    }
});
});

page.jade

 h1= title

-  if (typeof form !== 'undefined')
form(method='post')
  != form
  input(type='submit')

page.jsont

      <html>
      <head>
      <title>Example Form</title>
      <style>
        form {
            border-top: 1px dashed #ccc;
            width: 375px;
        }
        label {
            position: relative;
            width: 200px;
            top: 6px;
        }
        .required label {
            font-weight: bold;
        }
        .field {
            border-bottom: 1px dashed #ccc;
            padding: 10px 5px 15px 5px;
            position: relative;
        }
        .field input, .field select, .field textarea {
            margin: -20px 0 0 175px;
            width: 180px;
        }
        .field textarea {
            margin-top: -15px;
        }
        .field input[type='checkbox'] {
            position: relative;
            margin-left: 32px;
            top: 4px;
        }
        .field fieldset input {
            margin: 0;
            width: 20px;
        }
        .error {
            background: #ffcccc;
        }
        .error_msg {
            color: red;
            margin: -15px 0 0 0;
            padding: 5px 0;
        }
        input[type='submit'] {
            margin-top: 20px;
        }
    </style>
   </head>
    <body>
    <h1>Example Form</h1>
    <form action="" method="GET">
        {form}
        <input type="submit" />
    </form>
 </body>
 </html>

页面错误

  ReferenceError: /home/manigandan/workspace/CreatingForm/page.jade:1
    > 1| h1= title
      2| 
      3|   - if (typeof form !== 'undefined')
      4|     form(method='post')

title is not defined
at eval at <anonymous>   (/home/manigandan/workspace/CreatingForm/node_modules/jade/lib/jade.js:176:8)
at /home/manigandan/workspace/CreatingForm/node_modules/jade/lib/jade.js:181:12
at Object.render (/home/manigandan/workspace/CreatingForm/node_modules/jade/lib/jade.js:216:14)
at View.engine (/home/manigandan/workspace/CreatingForm/node_modules/jade/lib/jade.js:243:13)
at View.render (/home/manigandan/workspace/CreatingForm/node_modules/express/lib/view.js:75:8)
at Function.render (/home/manigandan/workspace/CreatingForm/node_modules/express/lib/application.js:504:10)
at ServerResponse.render (/home/manigandan/workspace/CreatingForm/node_modules/express/lib/response.js:753:7)
at /home/manigandan/workspace/CreatingForm/app.js:34:9
at callbacks (/home/manigandan/workspace/CreatingForm/node_modules/express/lib/router/index.js:161:37)
at param (/home/manigandan/workspace/CreatingForm/node_modules/express/lib/router/index.js:135:11)
4

1 回答 1

0

这与 无关forms,而是与您将参数传递给的方式有关render

app.get('/', function (req, res) {
  res.render('page', {
    title: 'Filling out the form...',
    form: reg_form.toHTML()
  });
});

locals请注意如何删除多余的部分。

于 2013-03-26T07:39:03.600 回答