gpt4 book ai didi

mysql - Sequelize 获取 MySql 存储过程数据作为数组?

转载 作者:行者123 更新时间:2023-11-30 21:47:25 25 4
gpt4 key购买 nike

如果我在 sequelize 中调用一个存储过程,像这样:

var query = "CALL GetSatellites();"
sequelize.query(query, { type: sequelize.QueryTypes.SELECT})
.spread(responseWithResult(res))
.catch(handleError(res));

和 .json() 转换它,我得到:

{
"0": {
"timestamp": "2018-02-13T00:00:00.000Z",
"severity": "critical",
"device": "battery",
"identifier": "P2",
"name": "P2 Battery Recover",
"detail": ""
},
"1": {
"timestamp": "2018-02-13T00:00:00.000Z",
"severity": "critical",
"device": "battery",
"identifier": "P3",
"name": "P3 Battery Recover",
"detail": ""
}
}

但我真的很喜欢它来执行您在进行非常相似的 SELECT 查询时获得的更常见的输出:

var query = "SELECT * FROM Satellites;"
sequelize.query(query, { type: sequelize.QueryTypes.SELECT})
.then(responseWithResult(res))
.catch(handleError(res));

当 .json() 时,我得到:

[
{
"_id": 4,
"SatelliteId": 1,
"createdAt": "2018-02-12T00:00:00.000Z",
"updatedAt": "2018-02-12T00:00:00.000Z",
"satelliteUTCTime": "2018-02-12T00:00:00.000Z",
"identifier": "P1",
"name": "P1 Battery Recover",
"severity": "critical",
"device": "battery",
"detail": "Kablooee!"
},
{
"_id": 5,
"SatelliteId": 1,
"createdAt": "2018-02-13T00:00:00.000Z",
"updatedAt": "2018-02-13T00:00:00.000Z",
"satelliteUTCTime": "2018-02-13T00:00:00.000Z",
"identifier": "P2",
"name": "P2 Battery Recover",
"severity": "critical",
"device": "battery",
"detail": ""
}
]

我已经在使用 spread() 来获取存储过程返回的元数据,但是在 sequelize 中是否有一些设置或调用我可以用来从存储过程调用中获取干净的对象数组,就像我做的那样直接选择?

最佳答案

你应该使用 type: sequelize.QueryTypes.RAW.then 而不是 .spread。那对我有用。可能是因为没有为过程调用设置模型。

关于mysql - Sequelize 获取 MySql 存储过程数据作为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48819131/

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