gpt4 book ai didi

javascript - 每个连接请求 Node JS 和 SQL?

转载 作者:行者123 更新时间:2023-12-03 05:20:41 24 4
gpt4 key购买 nike

我似乎无法从一个数据库连接发出多个请求。它总是告诉我只能在登录状态下发出请求。

例如,如下面的代码所示:函数的 getCarIdandOwner 部分将正常触发。但是 getChargeRate 不会。

我尝试像这样组合它们:

connection.execSqlBatch(getcarIdandOwner, getChargeRate);

但是这也不起作用,因为它告诉我 getChargeRate 未定义。

使用 Visual Studio Community,安装 NPM:Underscore 和 Tedious(用于 sql)。现在只需将其作为控制台应用程序运行以进行测试。

var Connection = require('tedious').Connection;
var config = {
userName: 'user',
password: 'passs',
server: 'somewhere.database.windows.net',
options: {
encrypt: true,
database: 'db-Test',
useColumnNames: true
var connection = new Connection(config);

connection.on('connect', function (err) {
// If no error, then good to proceed.
console.log("Connected".green);
toll("******-b77c-40e0-8f26-d44e98bc7264", "be45c903-****-****-b6ba-4b2fefa3d6b0");
});

function toll(chipId, locId) {
var carId = '';
var userOwner = '';
var charge = '';
var userBalance = '';

getcarIdandOwner = new Request(`SELECT car_id, userOwner FROM Cars WHERE carChipId = '${chipId}'`, function (err) {
if (err) {
console.log(err);
}
});

getcarIdandOwner.on('row', function (columns) {
carId = columns.car_id.value;
userOwner = columns.userOwner.value;
console.log('carId: ', carId, ' userOwner: ', userOwner);
});
getcarIdandOwner.on('done', function (rowCount, more) {
console.log(rowCount + ' rows returned');
if (rowCount = 1) {
console.log('Car Rows Returned Ok'.green);
} else {
console.log('Fatal Error: More than 1 Car Row Returned'.red);
};
});
connection.execSqlBatch(getcarIdandOwner);

getChargeRate = new Request(`SELECT Charge FROM locations WHERE location_id = '${locId}'`, function (err) {
if (err) {
console.log(err);
}
});
getChargeRate.on('row', function (columns) {
charge = columns.charge.value;
console.log('Charging account: ', userOwner, '$', charge);
});

connection.execSqlBatch(getChargeRate);
}

最佳答案

http://tediousjs.github.io/tedious/api-connection.html 有一些文档其中指出:

Only one request at a time may be executed on a connection. Once a Request has been initiated (with callProcedure, execSql, or execSqlBatch), another should not be initiated until the Request's completion callback is called.

所以你的代码应该是这样的:

function toll(chipId, locId) {
var carId = '';
var userOwner = '';
var charge = '';
var userBalance = '';

getcarIdandOwner = new Request(`SELECT car_id, userOwner FROM Cars WHERE carChipId = '${chipId}'`, function (err) {
if (err) {
console.log(err);
} else {
getChargeRate = new Request(`SELECT Charge FROM locations WHERE location_id = '${locId}'`, function (err) {
if (err) {
console.log(err);
}
});
getChargeRate.on('row', function (columns) {
charge = columns.charge.value;
console.log('Charging account: ', userOwner, '$', charge);
});
connection.execSql(getChargeRate);
}
});

getcarIdandOwner.on('row', function (columns) {
carId = columns.car_id.value;
userOwner = columns.userOwner.value;
console.log('carId: ', carId, ' userOwner: ', userOwner);
});
getcarIdandOwner.on('done', function (rowCount, more) {
console.log(rowCount + ' rows returned');
if (rowCount = 1) {
console.log('Car Rows Returned Ok'.green);
} else {
console.log('Fatal Error: More than 1 Car Row Returned'.red);
};
});
connection.execSqlBatch(getcarIdandOwner);

}

关于javascript - 每个连接请求 Node JS 和 SQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41399882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com