使用 sequelize 和 express API 调用存储过程
Calling a stored procedure with sequelize and an express API
这可能很简单,但我一直无法正确拼凑。
我正在尝试使用 Sequelize NPM 调用我构建的存储过程,然后我想通过来自 express api 和 return 输出的 GET 请求来触发它api.
的过程
Sequelize 部分的代码如下所示....
// Testing stored procedure //
const Retrieve = (testName) => connection.testdata_connection.query("EXEC [SPROC] [INPUTS]")
module.exports = {
tests: Tests(),
retrieve: Retrieve()
};
这部分“connection.testdata_connection”只是建立我与数据库的连接,我已经测试过了,我知道这部分已经设置好了。
我希望能够用...
const query = require('./database/queries'); ///Imports sequelize queries
const app = express();
app.get('/decrypt', function(req,res){
query.retrieve()
})
})
这根本行不通。
现在如果我在查询文件中做这样的事情...
const Retrieve = async function() {
const decrypt = await connection.testdata_connection.query("EXEC [SPROC] [INPUT]")
console.log(decrypt)
}
module.exports = {
tests: Tests(),
retrieve: Retrieve()
};
这将在我启动服务器时使用正确的数据登录到我的控制台。当我用我的端点击中它时,我希望它这样做。
首先,你的函数应该导出但不执行:
// creating an async function (all i/o operations should be async).
const Retrieve = async(testName) => connection.testdata_connection.query("EXEC [SPROC] [INPUTS]")
module.exports = {
retrieve: Retrieve,
// retrieve: Retrieve() if you call the function with (), the function will be executed and we don't want that yet
};
现在可以在路由中调用了:
const query = require('./database/queries'); ///Imports sequelize queries
const app = express();
// the route is async because it is executing async code
app.get('/decrypt', async (req,res) => {
// waiting for the answer with await
const response = await query.retrieve();
// Doing something with the response
})
您仍然需要检查错误,但这是基础。
这可能很简单,但我一直无法正确拼凑。
我正在尝试使用 Sequelize NPM 调用我构建的存储过程,然后我想通过来自 express api 和 return 输出的 GET 请求来触发它api.
的过程Sequelize 部分的代码如下所示....
// Testing stored procedure //
const Retrieve = (testName) => connection.testdata_connection.query("EXEC [SPROC] [INPUTS]")
module.exports = {
tests: Tests(),
retrieve: Retrieve()
};
这部分“connection.testdata_connection”只是建立我与数据库的连接,我已经测试过了,我知道这部分已经设置好了。
我希望能够用...
const query = require('./database/queries'); ///Imports sequelize queries
const app = express();
app.get('/decrypt', function(req,res){
query.retrieve()
})
})
这根本行不通。
现在如果我在查询文件中做这样的事情...
const Retrieve = async function() {
const decrypt = await connection.testdata_connection.query("EXEC [SPROC] [INPUT]")
console.log(decrypt)
}
module.exports = {
tests: Tests(),
retrieve: Retrieve()
};
这将在我启动服务器时使用正确的数据登录到我的控制台。当我用我的端点击中它时,我希望它这样做。
首先,你的函数应该导出但不执行:
// creating an async function (all i/o operations should be async).
const Retrieve = async(testName) => connection.testdata_connection.query("EXEC [SPROC] [INPUTS]")
module.exports = {
retrieve: Retrieve,
// retrieve: Retrieve() if you call the function with (), the function will be executed and we don't want that yet
};
现在可以在路由中调用了:
const query = require('./database/queries'); ///Imports sequelize queries
const app = express();
// the route is async because it is executing async code
app.get('/decrypt', async (req,res) => {
// waiting for the answer with await
const response = await query.retrieve();
// Doing something with the response
})
您仍然需要检查错误,但这是基础。