简体   繁体   中英

My Jade template button won't submit. Any ideas?

I'm a node noob and am following a tutorial trying to build my first application. For some reason my button is not doing anything at all. I'm not even receiving a 404 error (which I presume would mean the post method was working at least).

Here is my jade file. 'newuser.jade'

extends layout

block content
  h1= title
  form#formAddUser( name="adduser",method="post",action="/adduser")
  input#inputUserName(type="text",placeholder="username",name="username")
  input#inputUserEmail(type="text",placeholder="useremail",name="useremail")
  button#btnSubmit(type="submit") submit

js file: 'index.js'

/* POST to Add User Service */
router.post('/adduser', function(req, res) {

    // Set our internal DB variable
    var db = req.db;

    // Get our form values. These rely on the "name" attributes
    var userName = req.body.username;
    var userEmail = req.body.useremail;

    // Set our collection
    var collection = db.get('usercollection');

    // Submit to the DB
    collection.insert({
        "username" : userName,
        "email" : userEmail
    }, function (err, doc) {
        if (err) {
            // If it failed, return error
            res.send("There was a problem adding the information to the database.");
        }
        else {
            // And forward to success page
            res.redirect("userlist");
        }
    });
});

You need to increase the indent level after form , otherwise the inputs and button don't become nested within the <form>...</form> tags:

form#formAddUser( name="adduser",method="post",action="/adduser")
  input#inputUserName(type="text",placeholder="username",name="username")
  input#inputUserEmail(type="text",placeholder="useremail",name="useremail")
  button#btnSubmit(type="submit") submit 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM