我在Postman上测试我的post方法。 每当我发布结果时,它都会显示空数组[]。 我控制台在节点端记录它,它也返回空数组。
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
username TEXT,
email TEXT,
password TEXT
)
INSERT INTO users (username, email, password) VALUES ($1, $2, $3);
app.post('/api/users', function(req, res, next) {
db.post_user([req.body.username, req.body.email, req.body.password], function(err, user) {
console.log(user);
if(err) res.status(500).send(err);
else res.json(user);
})
})
var express = require('express');
var bodyParser = require('body-parser');
var session = require('express-session');
var cors = require('cors');
var massive = require('massive');
var config = require('./config');
var app = express();
app.use(bodyParser.json());
app.use(cors());
var db = massive.connect({connectionString: config.connectionString}, function(err, localdb){
db = localdb;
app.set('db', db);
});
app.get('/api/users', function(req, res, next) {
db.get_users(function(err, users) {
if(err) res.status(500).json(err);
else res.json(users);
})
})
app.post('/api/users', function(req, res, next) {
db.post_user([req.body.username, req.body.email, req.body.password], function(err, user) {
console.log(req.body);
if(err) res.status(500).send(err);
else res.json(user);
})
})
app.listen(3000, function() {
console.log("I am listening");
})
答案 0 :(得分:0)
因为您需要执行以下操作:
let query = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)"
let values = [req.body.username, req.body.email, req.body.password]
connection.query(query, values).then(function(user...)
取决于您使用的库