gpt4 book ai didi

sequelize.js - sequelize 没有将关联的表数据作为对象返回

转载 作者:行者123 更新时间:2023-12-03 22:24:59 24 4
gpt4 key购买 nike

我在后端使用带有 sequelize.js 的羽毛
我有两个关联模型
lines.model.js

const lines = sequelizeClient.define(
"lines",
{
line_number: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
timestamps: false,
},
{
hooks: {
beforeCount(options) {
options.raw = true;
},
},
}
);

// eslint-disable-next-line no-unused-vars
lines.associate = function (models) {
const { machines } = models;
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
lines.hasMany(machines);
};

machine.model.js
const machines = sequelizeClient.define(
"machines",
{
machine_name: {
type: DataTypes.STRING,
allowNull: true,
},
lineId: {
type: DataTypes.INTEGER,
allowNull: true,
},
url: {
type: DataTypes.STRING,
allowNull: true,
},
},
{
hooks: {
beforeCount(options) {
options.raw = true;
},
},
}
);

// eslint-disable-next-line no-unused-vars
machines.associate = function (models) {
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
const { lines } = models;
machines.belongsTo(lines);
};
线路服务的钩子(Hook)函数内部
lines.hooks.js
module.exports = {
before: {
all: [authenticate("jwt")],
find: [
(context) => {
// Get the Sequelize instance. In the generated application via:
const sequelize = context.app.get("sequelizeClient");
const { machines } = sequelize.models;
sequelize.raw = true;
console.log(machines);
context.params.sequelize = {
include: [{ model: machines }],
};

return context;
},
],
get: [],
create: [],
update: [],
patch: [],
remove: [],
},

after: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: [],
},

error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: [],
},
};


当我通过 postman ( url:[GET] http://localhost:3030/lines )调用线路服务的其余 api 时
它返回
{
"total": 4,
"limit": 10,
"skip": 0,
"data": [
{
"id": "1",
"line_number": "M11",
"machines.id": null,
"machines.machine_name": null,
"machines.lineId": null,
"machines.url": null,

},
{
"id": "5",
"line_number": "M22",
"machines.id": null,
"machines.machine_name": null,
"machines.lineId": null,
"machines.url": null,

},
{
"id": "2",
"line_number": "M12",
"machines.id": null,
"machines.machine_name": null,
"machines.lineId": null,
"machines.url": null,

},
{
"id": "3",
"line_number": "M21",
"machines.id": 16,
"machines.machine_name": "MULTI4",
"machines.lineId": 3,
"machines.url": "http://192.168.10.5/webservice/cwebservice.asmx?wsdl",

},
{
"id": "3",
"line_number": "M21",
"machines.id": 15,
"machines.machine_name": "MCAL4",
"machines.lineId": 3,
"machines.url": "http://192.168.10.4/webservice/cwebservice.asmx?wsdl",

}
]
}
如您所见,机器已添加到生产线中,如果生产线有多台机器,它将返回两次或更多次
我怎样才能使它像这样
{
"total": 4,
"limit": 10,
"skip": 0,
"data": [
{
"id": "1",
"line_number": "M11",
"machines" : []
},
{
"id": "5",
"line_number": "M22",
"machines" : []
},
{
"id": "2",
"line_number": "M12",
"machines" : []
},
{
"id": "3",
"line_number": "M21",
"machines" : [{
{
"id": 16,
"machine_name": "MULTI4",
"lineId": 3,
"url": "http://192.168.10.5/webservice/cwebservice.asmx?wsdl",
},
{
"id": 15,
"machine_name": "MCAL4",
"lineId": 3,
"url": "http://192.168.10.4/webservice/cwebservice.asmx?wsdl",
}
}]

}
]
}

最佳答案

我通过在钩子(Hook)函数中添加以下行找到了解决方案

Object.assign(context.params.sequelize, { raw: false });
所以钩子(Hook)文件变成了这样
module.exports = {
before: {
all: [authenticate("jwt")],
find: [
(context) => {
// Get the Sequelize instance. In the generated application via:
const sequelize = context.app.get("sequelizeClient");
const { machines } = sequelize.models;
sequelize.raw = true;
context.params.sequelize = {
include: [{ model: machines, nested: true }],
};
Object.assign(context.params.sequelize, { raw: false });
return context;
},
],
get: [],
create: [],
update: [],
patch: [],
remove: [],
},

after: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: [],
},

error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: [],
},
};

关于sequelize.js - sequelize 没有将关联的表数据作为对象返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64891516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com