如果我以续集的方式调用存储过程,例如:
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": ""
}
}
,但是我真的希望它做一个非常相似的选择查询时获得的更常见的输出:
var query = "SELECT * FROM Satellites;"
sequelize.query(query, { type: sequelize.QueryTypes.SELECT})
.then(responseWithResult(res))
.catch(handleError(res));
哪个,当.json(('d,我得到:
[
{
"_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": ""
}
]
我已经在使用vrize((来超越存储过程返回的元数据,但是我可以使用一些设置或续集,我可以用来从存储过程中获取清洁的对象,就像我一样直接选择?
您应该使用类型:sequelize.QueryTypes.RAW
和.then
而不是.spread
。那对我有用。可能是因为未为过程调用设置模型。
您必须在sp。
中设置一个参数这是一个演示代码:
sequelize.query('SET @outputData = null; CALL stored_procedure (:someInputData, @outputData); SELECT @outputData;',
{
replacements: {
someInputData: 'Send Your Data Here',
},
type: sequelize.QueryTypes.RAW,
})
.spread(response => console.log(response, 'response'))
.error((error) => {
console.log(error, 'errored');
});
注意:在创建实例时,添加'乘法:true'在您的续集中,例如:
const sequelize = new Sequelize(config.database.dbName, config.database.username, config.database.password,
{
host: config.database.host,
dialect: config.database.dialect,
logging: false,
dialectOptions: {
multipleStatements: true,
},
});