为什么这种连接和发送SQL请求的方法不起作用,而另一种方法起作用?

huangapple go评论100阅读模式
英文:

Why does this method of connecting and sending a SQL request not work but this other one does?

问题

这个有问题的部分来自mssql package page。为什么这个在我的机器上不起作用?我在这里做错了什么?

这个不起作用的部分使用了async,我应该在什么时候使用它?

这个不起作用的部分使localhost:5000不加载任何内容。然而,起作用的部分成功返回了SP的结果:

{"recordsets":[[{"SomeTimestamp":"2023-06-21T11:32:03.827Z","OtherField":""}]],"recordset":[{"SomeTimestamp":"2023-06-21T11:32:03.827Z","OtherField":""}],"output":{},"rowsAffected":[1,1]}

英文:

The surprising part is that the one that doesn't work is from the mssql package page. Why is this one not working on my machine? What am I doing wrong here? The one that works is some example I found elsewhere online and it works great.

As a side note - do I need to asynchronously do these requests? The one that doesn't work uses async, when should I be using this?

  1. var express = require('express');
  2. var app = express();
  3. const sql = require("mssql");
  4. //Configuration for the database
  5. const config = {
  6. user: 'sa',
  7. password: 'password',
  8. server: 'ipAddress',
  9. database: 'DBName',
  10. trustServerCertificate: true, //Only setting this to true for testing on a local test server; Change to false when deploying to live
  11. encrypt: true,
  12. };
  13. app.get('/', function (req, res) {
  14. //THIS DOESNT WORK
  15. async () => {
  16. try {
  17. await sql.connect(config)
  18. var request = new sql.Request();
  19. const result = await request.query(`exec spTest null`)
  20. console.dir(result)
  21. res.send(result.recordset)
  22. } catch (err) {
  23. // ... error checks
  24. console.dir(err)
  25. }
  26. }
  27. //THIS ONE WORKS!
  28. // sql.connect(config, function (err) {
  29. // if (err) console.log(err);
  30. // // create Request object
  31. // var request = new sql.Request();
  32. // // query to the database and get the records
  33. // request.query('exec spTest null ', function (err, recordset) {
  34. // if (err) console.log(err)
  35. // // send records as a response
  36. // console.log(recordset)
  37. // res.send(recordset);
  38. // });
  39. // });
  40. });
  41. var server = app.listen(5000, function () {
  42. console.log('Server is running...');
  43. });

The one that doesn't work makes localhost:5000 not load anything. However, the one that works successfully returns the results of the SP:

  1. {"recordsets":[[{"SomeTimestamp":"2023-06-21T11:32:03.827Z","OtherField":""}]],"recordset":[{"SomeTimestamp":"2023-06-21T11:32:03.827Z","OtherField":""}],"output":{},"rowsAffected":[1,1]}

答案1

得分: 1

以下是翻译好的部分:

看起来“不工作”的代码是在请求处理程序函数内定义async函数,并且在请求该路由时可能不会“调用”。

尝试将其更改为如下所示:

  1. app.get('/', async (req, res) => {
  2. try {
  3. await sql.connect(config)
  4. var request = new sql.Request();
  5. const result = await request.query(`exec spTest null`)
  6. console.dir(result)
  7. res.send(result.recordset)
  8. } catch (err) {
  9. // ... error checks
  10. console.dir(err)
  11. }
  12. }
英文:

It seems the "non-working" code is defining async function within the request handler function and may not be "invoked" when requesting that route.

Try changing it to something like this

  1. app.get('/', async (req, res) => {
  2. try {
  3. await sql.connect(config)
  4. var request = new sql.Request();
  5. const result = await request.query(`exec spTest null`)
  6. console.dir(result)
  7. res.send(result.recordset)
  8. } catch (err) {
  9. // ... error checks
  10. console.dir(err)
  11. }
  12. }

huangapple
  • 本文由 发表于 2023年6月22日 00:49:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定