javascript - 如何格式化此关联获取 Sequelize

标签 javascript node.js sequelize.js

我有一个多对多的关联,有两个表,products 和 orders。在我的数据透视表中,我保存了产品的 ID、数量和价格。当我获取产品时,我需要该产品的名称,但要获取名称,我需要在产品表中获取。我获取返回的响应是这样的

{
    "id": 111,
    "name": "Matheus",
    "phonenumber": "69993750103",
    "reference": null,
    "value_subtotal": "10.000",
    "value_delivery": "5.000",
    "value_total": "15.000",
    "status": "pending",
    "products": [
        {
            "name": "Açai 350ml",
            "OrdersProducts": {
                "quantity": 2,
                "price": "0.000"
            }
        },
        {
            "name": "acai 350ml",
            "OrdersProducts": {
                "quantity": 3,
                "price": "0.000"
            }
        }
    ]
}

但我需要这种格式的json

{
    "id": 111,
    "name": "Matheus",
    "street": "Rua olavo bilac",
    "phonenumber": "69993750103",
    "number": "3511",
    "reference": null,
    "note": "Retirar o morango",
    "value_subtotal": "10.000",
    "value_delivery": "5.000",
    "value_total": "15.000",
    "status": "pending",
    "createdAt": "2021-10-20T18:26:25.000Z",
    "updatedAt": "2021-10-20T18:26:25.000Z",
    "products": [
        {
            "name": "Açai 350ml",
            // here the difference, i want create a single object in the return, with all data i need of the product
            "quantity": 2,
            "price": "0.000"
            }
        },
        {
            "name": "acai 350ml",
            "quantity": 3,
            "price": "0.000"
            
        }
    ]
}

我的 Controller

 async getOrder(req, res) {
        const { id } = req.params;

        const order = await Orders.findByPk(id, {include: [{
            association: 'products',
            attributes: ['name'],
            through: {
                attributes:['quantity', 'price'],
                
                
            },
            raw: true,
        }]})
        if (!order) return res.status(404).send({ message: 'order ${`id`}' })
        return res.json(order);
    },

最佳答案

var a = {
  "id": 111,
  "name": "Matheus",
  "phonenumber": "69993750103",
  "reference": null,
  "value_subtotal": "10.000",
  "value_delivery": "5.000",
  "value_total": "15.000",
  "status": "pending",
  "products": [{
      "name": "Açai 350ml",
      "OrdersProducts": {
        "quantity": 2,
        "price": "0.000"
      }
    },
    {
      "name": "acai 350ml",
      "OrdersProducts": {
        "quantity": 3,
        "price": "0.000"
      }
    }
  ]
};

var orders = [];
orders.push(a);

var updatedOrders = orders.map(function(order) {
  order.products.forEach(function(product) {
    product.price = product.OrdersProducts.price;
    product.quantity = product.OrdersProducts.quantity;
    delete product.OrdersProducts;
  });
  return order;
});
console.log(updatedOrders);

关于javascript - 如何格式化此关联获取 Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69710076/

相关文章:

javascript - 如何在 javascript 全局命名空间中 stub /模拟函数

JavaScript 使用 3 轴鼠标 Canvas 绘制

javascript - Typeahead 模糊事件未在 iPhone 上触发

javascript - 查询 sequelize 中的关联位置?

node.js - Sequelize : "Cannot read property ' length' of undefined"

Node.js Sequelize 模型,其中一个字段是一组对象

javascript - UIWebView + 自定义 JavaScript 对象

javascript - 类型保护错误 TS2532 之后, typescript 对象可能未定义

node.js - 将电话音频流引入 Node.js 环境的简单方法

node.js - 如何在AWS s3上存储图像缩略图