Sequelize app.post:为什么“ TypeError:无法读取未定义的属性'create'”?

时间:2019-10-18 10:34:53

标签: javascript node.js sequelize.js

我想发布一个帖子请求,发布我测试了我到邮递员路线的产品,但是它不起作用。

关于邮递员,我只有:

  

{}

当我做一个console.log(必需的正文)时,我得到了数据。

在我的终端上,我出现此错误:

  

TypeError:无法读取未定义的属性“ create”

我的路线发布请求:

routes/Product/addProduct.js-这就是发生错误的地方。

const { Order } = require('../../models/Product/order');
const { Product } = require('../../models/Product/product');
const { ProductOrder } = require('../../models/Product/productOrder');

module.exports = app => {
  app.post('/orders/create', (async (req, res) => {

    try {
      // Create and save the order
      const savedOrder = await Order.create(req.body, { w: 1 }, { returning: true });

      // Loop through all the items in req.products
      req.body.products.forEach((item) => {

        // Search for the product with the givenId and make sure it exists. If it doesn't, respond with status 400.
        const product = Product.findById(item.id);
        if (!product) {
          return res.status(400);
        }

        // Create a dictionary with which to create the ProductOrder
        const po = {
          orderId: savedOrder.id,
          productId: item.id,
          qty: item.qty,
        }

        // Create and save a productOrder
        const savedProductOrder = ProductOrder.create(po, { w: 1 }, { returning: true });
      });

      // If everything goes well, respond with the order
      return res.status(200).json(savedOrder)
    }
    catch (error) {
      console.error("Order creation server error: ", error);
      res.status(500).send(error)
    };
  }));
}

我的模型:

models/Product/order.js

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Order = sequelize.define('Order', {
    uuid: DataTypes.UUID,
    address: DataTypes.STRING
  }, {});
  Order.associate = function(models) {
    Order.belongsToMany(models.Product, {
      through: 'ProductOrders',
      as: 'products',
      foreignKey: 'orderId',
      otherKey: 'productId'
    });
  };
  return Order;
};

models/Product/product.js

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Product = sequelize.define('Product', {
    uuid: DataTypes.UUID,
    title: DataTypes.STRING,
    description: DataTypes.TEXT,
    price: DataTypes.DECIMAL,
  }, {});
  Product.associate = function(models) {
    Product.belongsToMany(models.Order, {
      through: 'ProductOrders',
      as: 'orders',
      foreignKey: 'productId',
      // otherKey: 'orderId'
    });
  };
  return Product;
};

models/Product/productOrder.js

'use strict';
module.exports = (sequelize, DataTypes) => {
  const ProductOrder = sequelize.define('ProductOrder', {
    uuid: DataTypes.UUID,
    productId: DataTypes.UUID,
    orderId: DataTypes.UUID,
    quantity: DataTypes.DECIMAL
  }, {});
  ProductOrder.associate = function(models) {
    // associations can be defined here
  };
  return ProductOrder;
};

1 个答案:

答案 0 :(得分:0)

您正在导出函数,并试图将其分解为一个对象。尝试以下代码:

$text = 'Hello brother, your child was born. Congratulations.';
$words = array("Tebrikler","congratulations","felicitaciones");
$words = array_map('preg_quote', $words);
$pattern = "/\b(" . implode('|', $words) . ")\b/i";
$text = preg_replace($pattern, "<div class=\"cong\">$1</div>", $text);