简体   繁体   中英

error when creating post function for rest api

When I try to use the post function for express rest API, I keep getting errors like this.

events.js:85
      throw er; // Unhandled 'error' event
        ^
error: column "newuser" does not exist
at Connection.parseE (/Users/Chris/project/opshun/api/node_modules/pg/lib/connection.js:561:11)
at Connection.parseMessage (/Users/Chris/project/opshun/api/node_modules/pg/lib/connection.js:390:17)
at null.<anonymous> (/Users/Chris/project/opshun/api/node_modules/pg/lib/connection.js:92:20)
at TLSSocket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at TLSSocket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:529:20)

And the related code is here:

app.get('/form', function(req, res) {
    fs.readFile('./sign_up.html', function(error, content) {
        if (error) {
            res.writeHead(500);
            res.end();
        }
        else {
            res.writeHead(200, { 'Content-Type': 'text/html' });
            res.end(content, 'utf-8');
        }
    });
});

app.post('/signup', function(req, res) {

    var newuser = req.body.newuser;
    var newpass = req.body.newpass;
    pg.connect(connString, function(err, client, done) {
        client.query("INSERT INTO users(username, password) 
               values(newuser, newpass)");
    }
}

And the HTML file look like this:

<form action="/signup" method="post">
    <div>
        <label>Username:</label>
        <input type="text" name="newuser"/><br/>
    </div>
    <div>
        <label>Password:</label>
        <input type="password" name="newpass"/>
    </div>
    <div><input type="submit" value="Sign Up"/></div>
</form>

From the error message, error: column "newuser" does not exist . Do you have the column in the db?

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