trying to connect SQL using node js - node.js

app.get('/', function (req, res) {
var config = {
user: 'sa',
password: '',
server: 'localhost',
database: 'TestDB'
};
// connect to your database
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('select * from userInfo', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
console.log(recordset);
});
});
});
Getting this error in command prompt
{ Error: Failed to connect to localhost:1433 - Could not connect (sequence)
at Connection.tedious.once.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:216:17)
at Object.onceWrapper (events.js:293:19)
at emitOne (events.js:96:13)
at Connection.emit (events.js:191:7)
at Connection.socketError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:875:14)
at D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:740:25
at SequentialConnectionStrategy.connect (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connector.js:153:9)
at Socket.onError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connector.js:169:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
code: 'ESOCKET',
originalError:
{ ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
at ConnectionError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\errors.js:12:12)
at Connection.socketError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:875:30)
at D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:740:25
at SequentialConnectionStrategy.connect (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connector.js:153:9)
at Socket.onError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connector.js:169:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at emitErrorNT (net.js:1284:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
message: 'Failed to connect to localhost:1433 - Could not connect (sequence)',
code: 'ESOCKET' },
name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1299:37)
at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:497:11)
at Request.query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1242:12)
at D:\Nodejs\UsersCreate\app.js:118:17
at _poolCreate.then.catch.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:269:7)
at process._tickCallback (internal/process/next_tick.js:109:7) code: 'ECONNCLOSED', name: 'ConnectionError' }
undefined
Please help me on this error and need navigation to rectify this error

Related

Node Server ECONNRESET error occurs after running a sql request

I'm making a node server using socket.js and a MYSQL DB,
socket.on("signUp", (userSignUp) => {
console.log(userSignUp);
con.connect(function() {
con.query("SELECT COUNT(*) AS count FROM users WHERE username = '" + userSignUp.username + "'", function (err, result, fields) {
console.log(result[0].count);
if (result[0].count >= 1) {
socket.emit("signUpError", 'user-allready-exists');
return;
}
});
});
});
When I run this function, it successfully gets the data from the db however after aproximatly 30 seconds, it gives me this error>>>
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:217:20)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (D:\Projects\server\node_modules\mysql\lib\Connection.js:423:8)
at Protocol.emit (node:events:513:28)
at Protocol._delegateError (D:\Projects\server\node_modules\mysql\lib\protocol\Protocol.js:398:10)
at Protocol.handleNetworkError (D:\Projects\server\node_modules\mysql\lib\protocol\Protocol.js:371:10)
at Connection._handleNetworkError (D:\Projects\server\node_modules\mysql\lib\Connection.js:418:18)
at Socket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}
I have had a good look around, but i can't seem to find what is causing this issue. Any help would be greatly appreciated.

ELOGIN error while connecting to the SQL server

I'm learning full stack web development and was trying to connect to SQL server from my backend Node.js. I was following an online video. While running the index.js file I get the below error -
ConnectionError: Login failed for user 'systemadmin'.
at C:\Users\akunjalw\Desktop\FullStack\server\node_modules\mssql\lib\tedious\connection-pool.js:70:17
at Connection.onConnect (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\connection.js:1038:9)
at Object.onceWrapper (node:events:640:26)
at Connection.emit (node:events:520:28)
at Connection.emit (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\connection.js:1066:18)
at Parser.<anonymous> (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\connection.js:2574:20)
at Object.onceWrapper (node:events:639:28)
at Parser.emit (node:events:520:28)
at Readable.<anonymous> (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\token\token-stream-parser.js:32:12)
at Readable.emit (node:events:520:28) {
code: 'ELOGIN',
originalError: ConnectionError: Login failed for user 'systemadmin'.
at Login7TokenHandler.onErrorMessage (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\token\handler.js:239:19)
at Readable.<anonymous> (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\token\token-stream-parser.js:26:33)
at Readable.emit (node:events:520:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Readable.push (node:internal/streams/readable:228:10)
at next (node:internal/streams/from:98:31)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 'ELOGIN',
isTransient: undefined
}
}
undefined
[nodemon] clean exit - waiting for changes before restart
The code is as follows
const sql = require("mssql");
const config = {
user: "systemadmin",
password: "R#jasthaan1212",
server: "localhost",
database: "ORG_EMPLOYEEDATA",
options: {
trustedconnection: true,
trustServerCertificate: true,
enableArithAbort: true,
instancename: "SQL2019",
},
port: 50685,
};
async function getEmployeeName() {
try {
let pool = await sql.connect(config);
let employeeData = await pool
.request()
.query("select * from dbo.EMPLOYEES_DATA");
return employeeData.recordsets;
} catch (error) {
console.log(error);
}
}
module.exports = { getEmployeeName: getEmployeeName };
const dboperations = require("./dboperations");
dboperations.getEmployeeName().then((result) => {
console.log(result);
});
Please let me know what exactly I'm missing here. I couldn't find the way to resolve this by searching in internet as well, may be I'm terrible at searching. Any help to resolve this is appreciated.

Unknown command when using promise-ftp in node

I'm trying to use a promise approach when interacting with my ftp server. I'm using the node package 'promise-ftp' v1.3.5.
The code I have is as follows:
const appFeedBuffer = Buffer.from('somedatastring', 'utf8');
var Client = require('promise-ftp');
var ftpClient = new Client();
ftpClient.connect({ host: 'myHost', user: 'me', password: 'mypassword' })
.then(() => {
return ftpClient.mkdir('newDir')
}).then(() => {
return ftpClient.put(appFeedBuffer, 'fileName')
}).then(()=> {
return ftpClient.rmdir('oldDir')
}).then(() => {
return ftpClient.rename('newDir', 'oldDir')
}).then(() => {
return ftpClient.end();
}).catch(err => {
console.log("something went wrong: " + err);
res.status(500).send('SERVER ERROR');
});
The problem is the put command - I get the error 'UNKNOWN COMMAND' which appears to come from the #icetee/ftp library, of which promise-ftp is a dependant. put is most certainly a valid command in the promise-ftp library!
Stack:
Unhandled rejection Error: Unknown command
at makeError (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/connection.js:1128:13)
at Parser.<anonymous> (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/connection.js:122:25)
at emitTwo (events.js:126:13)
at Parser.emit (events.js:214:7)
at Parser._write (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/parser.js:61:10)
at doWrite (_stream_writable.js:397:12)
at writeOrBuffer (_stream_writable.js:383:5)
at Parser.Writable.write (_stream_writable.js:290:11)
at Socket.ondata (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/connection.js:298:20)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:597:20)
I can't find anyone else having this problem. What am I doing wrong?

Getting error while connecting database with nodejs

app.get('/', function (req, res) {
var config = {
//user: 'sa',
// password: '',
server: 'localhost',
database: 'TestDB',
debug: true
};
// connect to your database
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('select * from userInfo', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
//console.log(recordset);
sql.close();
});
});
});
I want to connect without user and password and Getting this error while executing through command prompt and there is some trusted connection query I can't find the please help
Error: Login failed for user ''.
at Connection.tedious.once.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:216:17)
at Object.onceWrapper (events.js:293:19)
at emitOne (events.js:96:13)
at Connection.emit (events.js:191:7)
at Connection.processLogin7Response (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:1148:16)
at Connection.message (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:1660:14)
at Connection.dispatchEvent (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:863:38)
at MessageIO.<anonymous> (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:757:18)
at emitNone (events.js:86:13)
at MessageIO.emit (events.js:188:7)
code: 'ELOGIN',
originalError:
{ ConnectionError: Login failed for user ''.
at ConnectionError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\errors.js:12:12)
at Parser.<anonymous> (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:507:33)
at emitOne (events.js:96:13)
at Parser.emit (events.js:191:7)
at Parser.<anonymous> (D:\Nodejs\UsersCreate\node_modules\tedious\lib\token\token-stream-parser.js:54:15)
at emitOne (events.js:96:13)
at Parser.emit (events.js:191:7)
at addChunk (D:\Nodejs\UsersCreate\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:284:12)
at readableAddChunk (D:\Nodejs\UsersCreate\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:271:11)
at Parser.Readable.push (D:\Nodejs\UsersCreate\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:238:10) message: 'Login failed for user \'\'.', code: 'ELOGIN' },
name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1299:37)
at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:497:11)
at Request.query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1242:12)
at D:\Nodejs\UsersCreate\app.js:119:17
at _poolCreate.then.catch.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:269:7)
at process._tickCallback (internal/process/next_tick.js:109:7) code: 'ECONNCLOSED', name: 'ConnectionError' }
GET / 200 36.940 ms - -
Please help me on this and want to connect this database using window authentication
You can try with:
options: {
trustedConnection: true
}
First install NPM Package,
npm install msnodesqlv8
Then change your config variable as,
const sql = require('mssql/msnodesqlv8')
var config = {
server: 'localhost',
driver: 'msnodesqlv8',
database: 'TestDB',
debug: true,
options: {
trustedConnection: true
}
};

Cloud Functions for Firebase Image download function Error

I am building a web application using Firebase and the new feature Cloud Functions for Firebase. I have created a function that takes a URL and downloads the image into a 64-bit encoded string as below using the node modules request and request-promise-native:
module.exports = {
downloadImageFromUrl: function (url) {
var options = {
method: 'GET',
uri: url,
resolveWithFullResponse: true,
simple: false,
family: 4
};
return rp.get(options)
.then(function (res) {
return "data:" + res.headers["content-type"] + ";base64," + new Buffer(res.body).toString('base64');
})
.catch(function (error) {
console.log("ERROR GETTING image", error);
return error;
});
}
};
The top function works perfectly running locally but once on firebase it gives the error:
RequestError: Error: getaddrinfo EAI_AGAIN lh6.googleusercontent.com:443
at new RequestError (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/errors.js:14:15)
at Request.plumbing.callback (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:87:29)
at Request.RP$callback [as _callback] (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:46:31)
at self.callback (/user_code/node_modules/request/request.js:188:22)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at Request.onRequestError (/user_code/node_modules/request/request.js:884:8)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at TLSSocket.socketErrorListener (_http_client.js:310:9)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at connectErrorNT (net.js:1020:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
I am calling the function in the firebase auth trigger when a user is created as below:
exports.createUser = functions.auth.user().onCreate(event => {
if (event.data.photoURL) {
utils.downloadImageFromUrl(event.data.photoURL)
.then(function(res){
console.log("User Photo", res);
})
.catch(function(error){
console.log("Error", error);
})
}
});
Any help would be greatly appreciated.
Not entirely sure yet if this is the answer, but after reading the documentation, I read their free plan which says you cannot make any out bound requests. So I guess getting an image from a Url counts as an outbound request. After I start paying for their service, I will come back to verify if this was the problem.

Resources