req.app.post(...)create_event - 不是函数 - 为什么?

时间:2017-09-15 05:13:21

标签: javascript node.js express axios

我正在使用axios,node,express和sql向我的数据库发出post请求,但是我一直收到一条错误:“TypeError:req.app.post(...)。create_event不是一个功能“。我无法弄清楚我做错了什么。

这是我的代码:

来自我的包含我的axios调用的函数:

    handleNext = () => {
const {stepIndex} = this.state;
if (!this.state.loading) {
  this.dummyAsync(() => this.setState({
    loading: false,
    stepIndex: stepIndex + 1,
    finished: stepIndex === 3,
  }));
  if (stepIndex === 3){
    var event = {
        title: this.state.title,
        description: this.state.description,
        date: this.state.date,
        starttime: this.state.starttime,
        endtime: this.state.endtime,
        venue: this.state.venue, 
        address: this.state.address,
        city: this.state.city,
        zipcode: this.state.zipcode,
        imgUrl: this.state.imgUrl,
        perks: this.state.perks,
        category: this.state.value,
        quantofvols: this.state.quantofvols

    }

    axios.post('http://localhost:3001/api/event', event).then(res =>{
        console.log(res.data);

    })

从我的节点服务器:

    app.post('/api/event', ec.createEvent);

来自我的事件控制器(或'ec'):

   module.exports = {
createEvent: (req, res, next) => {
    let { title, description, date, starttime, endtime, venue, address, city, zipcode, imgUrl, perks, category, quantofvols } = req.body;

    req.app.post('db').create_event([req.body.title, req.body.description, req.body.date, req.body.starttime, req.body.endtime, req.body.venue, req.body.address, req.body.city, req.body.zipcode, req.body.imageurl, req.body.category, req.body.perks, req.body.quantofvols])
        .then(event => {
            console.log(req.body)
            res.status(200).send("Success")})
        .catch(() => res.status(500).send());

},

从我的SQL查询:

    INSERT INTO Events (title, description, date, starttime, endtime, venue, address, city, zipcode, imageurl, category, perks, quantofvols)
    VALUES
    ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)

1 个答案:

答案 0 :(得分:0)

问题在于它在事件控制器中应该是

    req.app.get('db').create_event

而不是

    req.app.post('db').create_event
傻了我。