本文介绍了Node.js,Express和Jade:从表单到数据库的插入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试以翡翠形式输入数据时,出现错误消息,提示它为空.有人可以帮我弄清楚问题出在哪里吗?

when i try to enter data in my jade form i get error message that says it is null. Can someone help me figuring out what the problem is?

app.js

    var express = require('express');
var pg = require('pg');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();
var conString = "postgres://abc:123@localhost/abc";
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only,
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

//app.get('/', routes.index);
app.get('/', function (req, res) {
  res.render('index',
  { title : 'Home' }
  )
});
app.get('/users', user.list);

app.post('/', function(req, res){
  var header = req.param('header', null);  // second parameter is default
  var body = req.param('body', null);
  console.log(header);
  console.log(body);
  pg.connect(conString, function(err, client, done, request, response) {
    client.on('drain', client.end.bind(client));//stänger av när alla queries är klara
    client.query("INSERT INTO post(member_id, title, body) VALUES ('1', $1, $2)", [header, body], function(err, result){
      if (err) {
                console.log(err);
               }
               else{
                res.send("success!");
               }
          });
    });
 });

http.createServer(app).listen(app.get('port'), function(){
 console.log('Express server listening on port ' + app.get('port'));
});

index.jade

index.jade

extends layout
block content
    h1= title
    p Welcome to #{title}
    form(action='/',method='post')
        input(type='text',id='header',placeholder='header')
        input(type='text',id='body',placeholder='body')
        input(type='submit',name='submit',value='Submit')

layout.jade

layout.jade

doctype html

    html
        head
            title= title
            link(rel='stylesheet',href='/stylesheets/style.css')
        body
            block content
            p whaddup

但是,如果我使用 curl --verbose -d'header = abcd& body = 1234'http://localhost:3000 ,它可以正常工作,因此可以肯定地说它是玉器的一部分,但是我不知道怎么了.我对Node.js和所有其他东西都是新手:)预先感谢.

However if I use curl --verbose -d 'header=abcd&body=1234' http://localhost:3000 it works fine, so im fairly certain it's the jade part, but i've no clue what's wrong. I am new to nodejs and all that :)thanks in advance.

推荐答案

它是与数据一起提交的表单控件的 name ,而不是 id .实际上,您的表单提交的唯一值是 submit 按钮的值.

It's the name of a form control that is submitted with the data, not the id. As it is, the only value your form is submitting is that of the submit button.

它应该看起来像这样:

 input(type='text', name='header', placeholder='header')
 input(type='text', name='body', placeholder='body')

这篇关于Node.js,Express和Jade:从表单到数据库的插入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 07:13
查看更多