我无法在nodeExpress中发送发布请求

时间:2019-04-12 11:57:18

标签: node.js express

我真的不知道这可能是什么问题,但是每次尝试发布时都会出现日志错误:ER_BAD_NULL_ERROR:“ idmedecin”列不能为空 我已经看到分配类似的问题(后要求中有麻烦),但是没有任何解决我的问题的方法,而且由于idmedcin显然不是NULL,所以我看不到自己在做什么错 reqLink使用= http:// 。***。107:3000 / rendezvousinsert?idmedecin = 1&idpatient = 1&date = 546654&etat = 1

  const mysql = require('mysql');

const express = require('express');

var app = express();

const bodyparser = require('body-parser');



app.use(bodyparser.json());



var mysqlConnection = mysql.createConnection({

    host: 'localhost',

    user: 'root',

    password: '',

    database: 'pim',

    multipleStatements: true

});



mysqlConnection.connect((err) => {

    if (!err)

        console.log('DB connection succeded.');

    else

        console.log('DB connection failed \n Error : ' + JSON.stringify(err, undefined, 2));

});





app.listen(3000, () => console.log('Express server is runnig at port no : 3000'));

app.post('/rendezvousinsert/',(req,res)=> {
    var post= {
      idmedecin : req.body.idmedecin ,
      idpatient : req.body.idpatient ,
      date :  req.body.date ,
      etat : req.body.etat
    };
    mysqlConnection.query('INSERT INTO rendezvous SET ?' , post, function(error) {
        if (error) {
            console.log(error.message);
        } else {
            console.log('success');
        }
    });
});

这是我的表结构table

2 个答案:

答案 0 :(得分:2)

您正在通过http://..***.107:3000/rendezvousinsert?idmedecin=1&idpatient=1&date=546654&etat=1 req.body发送数据,但是尝试通过idmedecin=1&idpatient=1&date=546654&etat=1

访问数据

req.query.idmedecin // 1 req.query.idpatient // 1 req.query.date // 546654 // etc 此数据可用

{
      "ref": "nice_readable_picture_choice_reference",
      "title": "Picture Choice Title",
      "type": "picture_choice",
      "properties": {
        "description": "Cool description for the picture choice",
        "randomize": true,
        "allow_multiple_selection": false,
        "allow_other_choice": true,
        "supersized": false,
        "show_labels": false,
        "choices": [
          {
            "ref": "foo_choice_ref1",
            "label": "Foo 1",
            "attachment": {
              "type": "image",
              "href": "https://images.typeform.com/images/4bcd3"
            }
          },
          {
            "ref": "foo_choice_ref2",
            "label": "Foo 2",
            "attachment": {
              "type": "image",
              "href": "https://images.typeform.com/images/4bcd3"
            }
          }
        ]
      },
      "validations": {
        "required": false
      }
    }

答案 1 :(得分:1)

您试图从请求正文中获取字段,但是在您的示例中,您将它们作为查询参数发送。

我认为正确的方法是将对象发送到POST正文中,而不是作为查询参数,但是如果您希望请求像示例中一样保留,则应如下所示:

var post= {
   idmedecin : req.query.idmedecin ,
   idpatient : req.query.idpatient ,
   date :  req.query.date ,
   etat : req.query.etat
 };