React和API之间的通信(nodejs)

时间:2018-12-18 02:54:39

标签: node.js reactjs

我正在尝试以一种简单的形式从我的API(nodejs)获得帖子。 非常简单:

在我的API中,我有这个:

app.post("/api/register", (req, res) => {
        console.log("the nickname post is " + req.body.nickname);
    });

我的反应形式:

class App extends Component {
  render() {
    return (
      <div>
        <form action="http://localhost:8080/api/register" method="post">
        <input type="text" name="nickname" /><br />
        <input type="submit" />
        </form>
      </div>
    );
  }
}

它表示nodejs无法读取属性req.body.nickname。 我可能什么也没得到,但是我不知道什么。

1 个答案:

答案 0 :(得分:0)

在W3学校中,表格看起来应该像这样-请注意您缺少value属性

mysql> describe room;
+---------------------------------+--------------+------+-----+---------+-------+
| Field                           | Type         | Null | Key | Default | Extra |
+---------------------------------+--------------+------+-----+---------+-------+
| identifier                      | int(11)      | NO   | PRI | NULL    |       |
| number                          | varchar(255) | YES  |     | NULL    |       |
| property_identifier             | int(11)      | YES  |     | NULL    |       |
| status_identifier               | int(11)      | YES  |     | NULL    |       |
| block_identifier                | int(11)      | YES  | MUL | NULL    |       |
| floor_identifier                | int(11)      | YES  | MUL | NULL    |       |
| occupant_reservation_identifier | int(11)      | YES  | MUL | NULL    |       |
| room_type_identifier            | int(11)      | YES  | MUL | NULL    |       |
+---------------------------------+--------------+------+-----+---------+-------+
8 rows in set (0.03 sec)

mysql> describe reservation_rooms;
+------------------------+---------+------+-----+---------+-------+
| Field                  | Type    | Null | Key | Default | Extra |
+------------------------+---------+------+-----+---------+-------+
| reservation_identifier | int(11) | NO   | PRI | NULL    |       |
| rooms_identifier       | int(11) | NO   | PRI | NULL    |       |
+------------------------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> describe reservation;
+---------------------+---------+------+-----+---------+-------+
| Field               | Type    | Null | Key | Default | Extra |
+---------------------+---------+------+-----+---------+-------+
| identifier          | int(11) | NO   | PRI | NULL    |       |
| adults              | int(11) | YES  |     | NULL    |       |
| arrival             | date    | YES  |     | NULL    |       |
| children            | int(11) | YES  |     | NULL    |       |
| departure           | date    | YES  |     | NULL    |       |
| nights              | int(11) | YES  |     | NULL    |       |
| reservation_status  | int(11) | YES  |     | NULL    |       |
| source              | int(11) | YES  |     | NULL    |       |
| guest_identifier    | int(11) | YES  | MUL | NULL    |       |
| property_identifier | int(11) | YES  | MUL | NULL    |       |
+---------------------+---------+------+-----+---------+-------+
10 rows in set (0.02 sec)

mysql> select * from reservation_rooms;
+------------------------+------------------+
| reservation_identifier | rooms_identifier |
+------------------------+------------------+
|                      6 |                4 |
+------------------------+------------------+
1 row in set (0.00 sec)

mysql> select * from reservation;
+------------+--------+------------+----------+------------+--------+--------------------+--------+------------------+---------------------+
| identifier | adults | arrival    | children | departure  | nights | reservation_status | source | guest_identifier | property_identifier |
+------------+--------+------------+----------+------------+--------+--------------------+--------+------------------+---------------------+
|          6 |      2 | 2018-12-27 |        2 | 2018-12-27 |      2 |                  1 |   NULL |                5 |                NULL |
+------------+--------+------------+----------+------------+--------+--------------------+--------+------------------+---------------------+
1 row in set (0.02 sec)

mysql> select * from rooms;
ERROR 1146 (42S02): Table 'onecube_pms.rooms' doesn't exist
mysql> select * from room;
+------------+--------+---------------------+-------------------+------------------+------------------+---------------------------------+----------------------+
| identifier | number | property_identifier | status_identifier | block_identifier | floor_identifier | occupant_reservation_identifier | room_type_identifier |
+------------+--------+---------------------+-------------------+------------------+------------------+---------------------------------+----------------------+
|          4 | 190    |                   1 |                 2 |                1 |                2 |                            NULL |                    3 |
+------------+--------+---------------------+-------------------+------------------+------------------+---------------------------------+----------------------+
1 row in set (0.00 sec)