javascript - 重命名与 Sequelize 的关联

标签 javascript node.js sequelize.js

鉴于这个协会

Product.Locations = Product.belongsToMany(ProductLocation, { 
   as: 'locations', 
   through: ProductStock,
   foreignKey: 'productId',
   otherKey: 'locationId'
});

加载了以下模型
const product = await Product.findById(123, { include: Product.Locations });
console.log(product);
// -> {
//   id: 123,
//   ....,
//   locations: [ {
//      ProductStock: { productId: 123, locationId: 456, stock: 99 },
//      id: 456,
//      ...
//   } ],
//   ...
// }

如何将名称 ProductStock 更改为 stock

最佳答案

试试这个代码:

    var str=
    {
      id: 123,
      locations: 
      [ 
        {
            ProductStock: { productId: 123, locationId: 456, stock: 99 },
            id: 456,
        },
      ],
    };
    var str2=
    {
      id:str.id,
      locations:
      [
        {
          stock:
          { 
            productId:str.locations[0].ProductStock.productId,
            locationId:str.locations[0].ProductStock.locationId,
            stock:str.locations[0].ProductStock.stock
          },
          id:str.locations[0].id
        }
      ]
    };
    console.log(JSON.stringify(str2));

关于javascript - 重命名与 Sequelize 的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58997827/

相关文章:

Javascript:选择元素的下一个兄弟并通过单击更改其类(不使用 ID)

JavaScript 数组操作

Sequelize.js:ER_ROW_IS_REFERENCED:无法删除或更新父行:外键约束失败

sql-server - Sequelize 不在控制台中显示 CREATE TABLE 语句

mysql - Sequelize top level where with Op.or 给出错误

javascript - Google 图表中的报价错误 "Data column(s) for axis #0 cannot be of type string"

javascript - 如何用循环过程停止setInterval

node.js - 无法使用gmail api Node js发送邮件

node.js - 何时将 React 与 npm 结合使用?

javascript - Node Express app.get(*) req.url 始终为/favion.ico