发布请求返回未定义的postgres

时间:2019-11-18 23:31:58

标签: postgresql express

我正在学习postgres,但是在发出发布请求时,控制台日志中没有定义我,我检查了所有内容,我猜这是一个错字

这是App.js

const express = require('express')
const app = express()
const path = require('path')
const port = 3000
var bodyParser = require('body-parser')
const connection = require('./Database/connection')
const routes = require("./Routes/routes");
app.use(express.static(path.join(__dirname, 'public')));
var handlebars = require('express-handlebars');

app.engine('handlebars', handlebars({ 
    defaultLayout: 'main', 
    layoutsDir: __dirname + '/views/layouts/' 
}));
app.set('views', path.join(__dirname, 'views')); 
app.set('view engine', 'handlebars'); 

app.use(bodyParser.urlencoded({
    extended: true
}));

app.use(bodyParser.json())

//DatabaseConnection
connection.connect(err => {
    if (err) {
        console.error('connection error', err.message)
    } else {
        console.log('connectedd')
    }
})
app.use("/", routes);
app.listen(port, () => console.log(`Example app listening on port ${port}!`))

这是CreatePost:

module.exports.CreatePost = (req,res) => {
    const {title,content,category} = req.body
    const sql = "INSERT INTO blogs(title,content,category) VALUES($1,$2,$3)";
    const values = [title,content,category];
    connection.query(sql,values)
        .then(res => console.log(res.rows[0]))
        .catch(e => console.error(e.stack))
}

预先感谢您的帮助

0 个答案:

没有答案