أهلا بالجميع،
لدي مشكلة في اتصال قاعدة البيانات. لقد عملت بشكل جيد لفترة من الوقت ، لكن منذ يوم أمس ، أواجه هذا الخطأ. ليس لدي أي فكرة عن السبب.
[خطأ في الطلب: لا يمكن إجراء الطلبات إلا في حالة "تسجيل الدخول" ، وليس الحالة النهائية]
connection.on('connect', function(err) {
Console.log("-------here------");
if(err) {
return console.error(err); // <--
connected = false;
} else {
connected = true;
}
});
أعتقد أنه لا يدخل في وظيفة رد الاتصال الخاصة بالاتصال. إنه لا يطبع أي شيء ويكون الاتصال دائمًا خاطئًا.
شكرا لك !!
لدي مشكلة مماثلة.
C:\Users\Austin\Desktop\prok>node prok.js
{ [RequestError: Requests can only be made in the LoggedIn state, not the Connecting state
]
message: 'Requests can only be made in the LoggedIn state, not the Connecting state',
code: 'EINVALIDSTATE' }
buffer.js:506
throw new RangeError('index out of range');
^
RangeError: index out of range
at checkOffset (buffer.js:506:11)
at Buffer.readUInt8 (buffer.js:544:5)
at Packet.isLast (C:\Users\Austin\Desktop\prok\node_modules\tedious\lib\packet.js:121:
29)
at ReadablePacketStream.<anonymous> (C:\Users\Austin\Desktop\prok\node_modules\tedious
\lib\message-io.js:102:18)
at ReadablePacketStream.emit (events.js:107:17)
at readableAddChunk (C:\Users\Austin\Desktop\prok\node_modules\tedious\node_modules\re
adable-stream\lib\_stream_readable.js:201:16)
at ReadablePacketStream.Readable.push (C:\Users\Austin\Desktop\prok\node_modules\tedio
us\node_modules\readable-stream\lib\_stream_readable.js:165:10)
at ReadablePacketStream.Transform.push (C:\Users\Austin\Desktop\prok\node_modules\tedi
ous\node_modules\readable-stream\lib\_stream_transform.js:133:32)
at ReadablePacketStream._transform (C:\Users\Austin\Desktop\prok\node_modules\tedious\
lib\message-io.js:68:16)
at ReadablePacketStream.Transform._read (C:\Users\Austin\Desktop\prok\node_modules\ted
ious\node_modules\readable-stream\lib\_stream_transform.js:172:10)
أنا أستخدم نموذج الكود الدقيق هنا إلى حد كبير: https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-nodejs-simple-linux/
--prok.js--
var Connection = require('tedious').Connection;
var config = {
userName: 'tester',
password: 'tester',
server: 'localhost',
options: {
port:3306
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
});
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
function executeStatement() {
request = new Request("SELECT * FROM testdb.testtable;", function(err) {
if (err) {
console.log(err);}
});
var result = "";
request.on('row', function(columns) {
console.log('row');
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
result+= column.value + " ";
}
});
console.log(result);
result ="";
});
request.on('done', function(rowCount, more) {
console.log(rowCount + ' rows returned');
});
connection.execSql(request);
}
executeStatement();
yossazou لا توجد معلومات كافية هنا لفهم ما يجري. الخطأ الذي تراه يعني أنك تحاول تنفيذ طلب بينما لم يعد مملاً متصلاً بقاعدة البيانات.
austincap أنت تقوم بتنفيذ طلب على الاتصال قبل إنشاء الاتصال. لهذا السبب تحصل على الخطأ Requests can only be made in the LoggedIn state, not the Connecting state
.
أنا مبتدئ في كل هذا ولكني وجدت أن إضافة معالج لحدث تصحيح أخطاء الاتصال ساعدني في هذا الخطأ بالضبط. اكتشفت أن اتصالي قد تم رفضه من قبل خادم SQL.
connection.on('debug', function(err) { console.log('debug:', err);});
فار اتصال = يتطلب ("مملة").
var Request = مطلوب ('tedious'). Request؛
فار صريح = يتطلب ("صريح") ؛
var bodyParser = يتطلب ("body-parser") ؛
تطبيق var = express () ؛
// Body Parser Middleware
app.use (bodyParser.json ()) ؛
// إعداد الخادم
var server = app.listen (process.env.PORT || 8081، function () {
var port = server.address (). port ؛
console.log ("التطبيق يعمل الآن على المنفذ" ، المنفذ) ؛
}) ؛
// إنشاء اتصال بقاعدة البيانات
فار التكوين = {
المصادقة: {
والخيارات: {
userName: "اسم المستخدم" ، // قم بتحديثي
كلمة المرور: 'password' // قم بتحديثي
} ،
اكتب: "افتراضي"
} ،
الخادم: 'servername.database.windows.net' ، // قم بتحديثي
والخيارات: {
قاعدة البيانات: "تطوير" ، // قم بتحديثي
التشفير: صحيح / مهم
}
}
/ / إنشاء الاتصال إلى اللازوردية
var connect = اتصال جديد (config) ؛
// إنشاء مجموعة json
بيانات var = []
الدالة executeStatement (استعلام ، الدقة) {
طلب فار = طلب جديد (استعلام ، وظيفة (خطأ ، عدد الصفوف ، صفوف) {
إذا (يخطئ) {
console.log (يخطئ) ؛
} آخر {
console.log (rowCount + 'rows') ؛
res.send ({status: 200، data: data، message: "OK"})
}
}) ؛
//connection.on('debug', function(err) { console.log('debug:', err);}); if the connection is not working
request.on('row', function (row) {
data.push({
ID: row[0].value,
ProductName: row[1].value,
Price: row[2].value,
ProductDescription: row[3].value,
CreatedDate: row[4].value,
Username: row[5].value
})
}) ؛
connect.execSql (طلب) ؛
}
app.get ('/ api / products'، function (req، res) {
var query = ('Select * from Products') ؛
executeStatement (استعلام ، الدقة) ؛
}) ؛
// لقد لاحظت أنه تمت إعادة تعيين كلمة مرور db وبيانات الاعتماد الخاصة بالمستخدم ، لذا لا يمكن إجراء اتصال te
التعليق الأكثر فائدة
أنا مبتدئ في كل هذا ولكني وجدت أن إضافة معالج لحدث تصحيح أخطاء الاتصال ساعدني في هذا الخطأ بالضبط. اكتشفت أن اتصالي قد تم رفضه من قبل خادم SQL.
connection.on('debug', function(err) { console.log('debug:', err);});