javascript 为什么带快速输出的NodeJS为1偏移量?

czq61nw1  于 2022-11-27  发布在  Java
关注(0)|答案(1)|浏览(104)

来自NodeJS的响应的偏移量为1。例如:1查询:输出:* 空响应 * 2查询:B输出:A 3查询:C输出:B
如果您需要API/JSON输出,请告诉我。
我觉得我只是在这里有一个逻辑问题。或者有这个偏移的命令吗?

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

var resultQueryFilter = "";
var search_param = "";

var Connection = require('tedious').Connection;  
var config = {  
    server: '172.17.0.2',  
    authentication: {
        type: 'default',
        options: {
            userName: 'sa',
            password: 'PW' 
        }
    },
    options: {
        encrypt: false,
        useUTC: true,
        database: 'Park'  
    }
}; 
var connection = new Connection(config);  
connection.on('connect', function(err) {  
    // If no error, then good to proceed. 
    if(err) {
        console.log('Error: ', err)
      } else {
        console.log("Successful connection");
      } 
});
  

connection.connect();

var Request = require('tedious').Request;  
var TYPES = require('tedious').TYPES;  

function executeStatementQuery() {  
  request = new Request("SELECT * from dbo.users WHERE first_name like '%" +search_param+ "%' FOR JSON PATH;", function(err) {  
  if (err) {  
      console.log(err);}  
  });  
  request.on('row', function(columns) {  
      columns.forEach(function(column) {  
        if (column.value === null) {  
          console.log('NULL');  
        } else {  
          resultQueryFilter+= column.value + " ";  
        }  
      });  
      //console.log(resultQueryFilter);  
  });  
  connection.execSql(request);  
}

const app = express();
app.get('/company/search', (req, res) => {
  search_param = String(req.query.company_name);

  if (!search_param) {
    //No search param send
    console.log("Search request ist empty. Returning HTTP-406");
    res.status(406).send("Forbidden."); //Forbidden, as query cannot be empty
  } else {
    console.log("API Call with search req");
    console.log("Going to execute query");
    executeStatementQuery();
    console.log("Executed Query");
    console.log("Returning Output as JSON: " + String(resultQueryFilter));
    res.send(resultQueryFilter);
    console.log("Clearing old params");
    search_param = "";
    resultQueryFilter = "";
    console.log("Request done");

  }
});

app.listen(PORT, HOST, () => {
  console.log(`Running on http://${HOST}:${PORT}`);
});

无法张贴其他信息:您的请求

wlzqhblo

wlzqhblo1#

您的代码使用了全局变量requestresultQueryFilter,您所做的所有查询都将同时使用这两个变量。这不能产生正确的(甚至是可预测的)结果。
使用局部变量:

app.get('/company/search', function(req, res) {
  var search_param = String(req.query.company_name);
  var request = new Request("SELECT * from dbo.users WHERE first_name like '%" +search_param+ "%' FOR JSON PATH;",
    function(err) {
      if (err) console.log(err);
    });
  var resultQueryFilter = "";
  request.on('row', function(columns) {
    columns.forEach(function(column) {
      if (column.value === null) {
        console.log('NULL');
      } else {
        resultQueryFilter += column.value + " ";
      }
    });
  });
  request.on('done', function() {
    res.send(resultQueryFilter);
  });
  connection.execSql(request);
});

相关问题