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

javascript node.js express axios

32 观看

1回复

0 作者的声誉

我正在使用axios,node,express和sql向我的数据库发出发布请求,但是我不断收到一条错误消息:“ 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)
作者: user8610619 的来源 发布者: 2017 年 9 月 15 日

回应 1


0

0 作者的声誉

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

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

并不是

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

愚蠢的我。

作者: user8610619 发布者: 2017 年 9 月 15 日
32x32