给出该关联
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
?
答案 0 :(得分:0)
只需尝试以下代码:
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));