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

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

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?

var express = require('express');
var app = express();
const sql = require("mssql");

//Configuration for the database
const config = {
    user: 'sa',
    password: 'password',
    server: 'ipAddress', 
    database: 'DBName',
    trustServerCertificate: true, //Only setting this to true for testing on a local test server; Change to false when deploying to live
    encrypt: true,
};

app.get('/', function (req, res) {

    //THIS DOESNT WORK
     async () => {
         try {
          await sql.connect(config)
          var request = new sql.Request();
          const result = await request.query(`exec spTest null`)
          console.dir(result)
          res.send(result.recordset)
         } catch (err) {
          // ... error checks
          console.dir(err)
         }
     }

    //THIS ONE WORKS!
    // sql.connect(config, function (err) {
    
        // if (err) console.log(err);

        // // create Request object
        // var request = new sql.Request();
           
        // // query to the database and get the records
        // request.query('exec spTest null ', function (err, recordset) {
            
            // if (err) console.log(err)

            // // send records as a response
			// console.log(recordset)
            // res.send(recordset);
            
        // });
    // });
});

var server = app.listen(5000, function () {
    console.log('Server is running...');
});

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

{"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函数,并且在请求该路由时可能不会“调用”。

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

app.get('/', async (req, res) => {
     try {
      await sql.connect(config)
      var request = new sql.Request();
      const result = await request.query(`exec spTest null`)
      console.dir(result)
      res.send(result.recordset)
     } catch (err) {
      // ... error checks
      console.dir(err)
     }
 }
英文:

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

app.get('/', async (req, res) => {
     try {
      await sql.connect(config)
      var request = new sql.Request();
      const result = await request.query(`exec spTest null`)
      console.dir(result)
      res.send(result.recordset)
     } catch (err) {
      // ... error checks
      console.dir(err)
     }
 }

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:

确定