首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >属性未定义,无法对其进行结构分析

属性未定义,无法对其进行结构分析
EN

Stack Overflow用户
提问于 2021-05-11 01:12:07
回答 1查看 140关注 0票数 0

我正在对mssql数据库使用sequelize。以下是订单的模型:

代码语言:javascript
运行
复制
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 post写成了以下内容:

代码语言:javascript
运行
复制
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或我的前端发帖时,我收到以下消息:

代码语言:javascript
运行
复制
 const { customerName, customerAddress, phoneNumber, email } = req.body;
          ^

TypeError: Cannot destructure property 'customerName' of 'req.body' as it is undefined.

这是我的前端:

代码语言:javascript
运行
复制
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(订单),所以我可以看到它实际上是一个包含数据的订单。这是我在console中得到的,所以我认为订单中有数据,所以req.body也应该有数据。{customerName: "testman", customerAddress: "test street", phoneNumber: "testnumber", email: "test@gmail.com"}

那么为什么我的数据库没有得到它应该得到的数据呢?

EN

回答 1

Stack Overflow用户

发布于 2021-05-11 02:01:17

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67474867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档