javascript - 无法解构属性,因为它是未定义的

标签 javascript reactjs http sequelize.js

我正在为 mssql 数据库使用 sequelize。
这是订单模型:

import Sequelize from "sequelize";
import { db } from "../config/database.js";

const Orders = db.define(
  "order",
  {
    customerName: {
      type: Sequelize.STRING,
    },
    customerAddress: {
      type: Sequelize.STRING,
    },
    phoneNumber: {
      type: Sequelize.STRING,
    },
    email: {
      type: Sequelize.STRING,
    },
  },
  {
    timestamps: false,
  }
);

export { Orders };
我将 http 帖子写成如下:
router.post("/orders", async (req, res) => {
  const { customerName, customerAddress, phoneNumber, email } = req.body;
  const order = await Orders.create({
    customerName,
    customerAddress,
    phoneNumber,
    email,
  });
  res.json(order);
}); 
但是当我尝试从 Postman 或我的前端发帖时,我收到以下消息:
 const { customerName, customerAddress, phoneNumber, email } = req.body;
          ^

TypeError: Cannot destructure property 'customerName' of 'req.body' as it is undefined.
这是我的前端:
function submit(e) {
    console.log(order);
    e.preventDefault();
    axios
      .post("http://localhost:3002/parts/orders", {
        customerName: order.customerName,
        customerAddress: order.customerAddress,
        phoneNumber: order.phoneNumber,
        email: order.email,
      })
      .then((res) => {
        console.log(res.data);
      });
  }

此函数由提交的表单使用。它将数据发送到/parts/orders 因为我改变了路由。我有 console.log(order) 所以我可以看到它确实是一个带有数据的订单。这是我在控制台中得到的,所以我想订单中有数据,req.body 也应该有数据。{customerName: "testman", customerAddress: "test street", phoneNumber: "testnumber", email: "test@gmail.com"}那么为什么我的数据库没有得到它应该得到的数据呢?

最佳答案

我没有app.user(express.json())在我的 index.js 文件中

关于javascript - 无法解构属性,因为它是未定义的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67474867/

相关文章:

javascript - 表格 Angular 过滤器

javascript - 一次性删除范围记录 - mongodb

ajax - Comet 与当前技术的兼容性

http - 我们可以使用 TLS over TCP 协议(protocol)吗?

javascript - 摆脱 jQuery 中的重复代码

javascript - jquery stopPropagation 停止 ajax get

.net - 从本地主机连接到 asp.net web api 时出现 SSL 错误

javascript - 为什么我在 React 应用程序中失去了登录信息?

json - 为什么从远程AWS S3服务器下载json文件时axios GET错误

asp.net - 重定向/发布到本地主机而不是外部域