Why Can't I connect SQL Server to my node js - javascript

I'm using the 'mssql' npm package to connect my SQL server to my node JS.
I went through a few questions, but it wasn't solved anywhere.
My SQL Server login credentials are-
server type : Database Engine
Server name : DESKTOP-MPF88ST\KUSHAL
authentication : sql server authentication
login : username
password : password
and the database I wanna get is schoolDB
and my code goes something like this
const sql =require("mssql")
const config = {
login: 'username',
password:'password',
server: 'localhost',
database: "schoolDB",
options : {
enableArithAbort: true
},
port : 1433
}
sql.connect(config, (err) => {
if(err){
console.log(err);
}
else {
console.log("Connected")
}
I have my TCP/IP enabled and set up, SQL Server Browser enabled, but it still throws the error-
ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
at Connection.<anonymous> (E:\Kushals\server\node_modules\mssql\lib\tedious\connection-pool.js:68:17)
at Object.onceWrapper (events.js:422:26)
at Connection.emit (events.js:315:20)
at Connection.socketError (E:\Kushals\server\node_modules\mssql\node_modules\tedious\lib\connection.js:1290:12)
at E:\Kushals\server\node_modules\mssql\node_modules\tedious\lib\connection.js:1116:21
at SequentialConnectionStrategy.connect (E:\Kushals\server\node_modules\mssql\node_modules\tedious\lib\connector.js:87:14)
at Socket.onError (E:\Kushals\server\node_modules\mssql\node_modules\tedious\lib\connector.js:100:12)
at Socket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3) {
code: 'ESOCKET',
originalError: ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
at ConnectionError (E:\Kushals\server\node_modules\mssql\node_modules\tedious\lib\errors.js:13:12)
at Connection.socketError (E:\Kushals\server\node_modules\mssql\node_modules\tedious\lib\connection.js:1290:56)
at E:\Kushals\server\node_modules\mssql\node_modules\tedious\lib\connection.js:1116:21
at SequentialConnectionStrategy.connect (E:\Kushals\server\node_modules\mssql\node_modules\tedious\lib\connector.js:87:14)
at Socket.onError (E:\Kushals\server\node_modules\mssql\node_modules\tedious\lib\connector.js:100:12)
at Socket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'ESOCKET'
}
}
How can I solve it ??

Try this:
const sql = require('mssql/msnodesqlv8')
const pool = new sql.ConnectionPool({
login: 'username',
password:'password',
database: 'schoolDB',
server: 'DESKTOP-MPF88ST\KUSHAL',
driver: 'msnodesqlv8',
options: {
trustedConnection: true
}
})
pool.connect().then(() => {
//sample query
pool.request().query('select 1 as number', (err, result) => {
console.dir(result)
})
})

Ensure that the TCP/IP protocol is Enabled as it is disabled by default.
Steps:
Go to Computer management
Under Service and Applications -> SQL Server Configuration Manager
-> SQL Server Network configuration -> click on "Protocols for MSSQLSERVER"
You will see TCP/IP section (to enable right click and enable it)

Related

MSSQL & Node.JS - Failed to connect to WIN-OTGKTVPUIFC:1433

I have a problem when connect to SQL Server.
My config connect SQL Server:
const mssql = require('mssql');
const configMsSql = {
server:"myserver",
port:myport,
user:"username",
password:"pass",
database:"dbname",
options:{
trustServerCertificate: true
}
};
async function connectState(){
const connect = await mssql.connect(configMsSql);
console.log("Connect to SQL successfully!");
return connect;
}
module.exports = connectState();
After run server. mssql throw error:
err = new ConnectionError(err)
^
ConnectionError: Failed to connect to WIN-OTGKTVPUIFC:1433 - 7C250000:error:0A000102:SSL routines:ssl_choose_client_version:unsupported protocol:c:\ws\deps\openssl\openssl\ssl\statem\statem_lib.c:1986:
at C:\UIS\SboxNode\node_modules\mssql\lib\tedious\connection-pool.js:70:17
at Connection.onConnect (C:\UIS\SboxNode\node_modules\tedious\lib\connection.js:1020:9)
at Object.onceWrapper (node:events:628:26)
at Connection.emit (node:events:513:28)
at Connection.emit (C:\UIS\SboxNode\node_modules\tedious\lib\connection.js:1048:18)
at Connection.socketError (C:\UIS\SboxNode\node_modules\tedious\lib\connection.js:1399:12)
at C:\UIS\SboxNode\node_modules\tedious\lib\connection.js:2406:25
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: 'ESOCKET',
originalError: ConnectionError: Failed to connect to WIN-OTGKTVPUIFC:1433 - 7C250000:error:0A000102:SSL routines:ssl_choose_client_version:unsupported protocol:c:\ws\deps\openssl\openssl\ssl\statem\statem_lib.c:1986:
at Connection.socketError (C:\UIS\SboxNode\node_modules\tedious\lib\connection.js:1399:28)
at C:\UIS\SboxNode\node_modules\tedious\lib\connection.js:2406:25
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: 'ESOCKET',
isTransient: undefined
}
}
Node.js v18.12.1
Although I enabled TCP/IP and field TCP Port used exactly.
I tried to search gg this error but the most answer that "enable TCP/IP".
I spent many time to fix but cant. Help me plzz :(

No stream features to proceed using node-xmpp-client

I've installed prosody on an ubuntu AWS server. I have tried using Adium client and swift, but I get the same error indicated in the title. I've tried using node-xmpp-client to create a client but same error.
Node-XMPP client code:
const Client = require('node-xmpp-client');
const options = {
jid: 'test#ec2-18-222-249-134.us-east-2.compute.amazonaws.com',
password: 'password',
host: 'ec2-18-222-249-134.us-east-2.compute.amazonaws.com'
}
const client = new Client(options);
client.once('online', connectionDetails =>{
console.log('We are connected');
console.log(connectionDetails);
});
Error Message:
Error: No stream features to proceed with
at Connection.onStanza (/Users/my-name/BeginXMPP/node_modules/node-xmpp-core/lib/Connection.js:363:17)
at StreamParser.<anonymous> (/Users/my-name/BeginXMPP/node_modules/node-xmpp-
core/lib/Connection.js:231:10)
at StreamParser.emit (events.js:198:13)
at SaxLtx.<anonymous> (/Users/my-name/BeginXMPP/node_modules/#xmpp/streamparser/index.js:69:14)
at SaxLtx.emit (events.js:198:13)
at SaxLtx._handleTagOpening (/Users/my-name/BeginXMPP/node_modules/ltx/lib/parsers/ltx.js:40:12)
at SaxLtx.write (/Users/my-name/BeginXMPP/node_modules/ltx/lib/parsers/ltx.js:159:18)
at StreamParser.write (/Users/my-name/BeginXMPP/node_modules/#xmpp/streamparser/index.js:134:17)
at Connection.onData (/Users/my-name/BeginXMPP/node_modules/node-xmpp-core/lib/Connection.js:310:17)
at Socket.emit (events.js:203:15)
Emitted 'error' event at:
at Client.<anonymous> (/Users/my-name/BeginXMPP/node_modules/node-xmpp-client/lib/Client.js:158:23)
at Connection.emit (events.js:203:15)

Requesting tables from MS Sql - Node.js web service

I am in need of assistance in terms of generating a request to my Microsoft SQL Server via a web service (Built in Node.js).
To summarize the problem at hand, it is as follows: I am currently logging into a MS Sql Server Management with a windows auth account - that is all find and dandy however, I am now trying to build a web service that allows for selects and transacts of some tables which is where I am running into issues now specifically in terms of logging in and pulling data to the web service.
Code
var express = require('express'); var app = express();
app.get('/',function(req,res) {
const sql = require('mssql');
// Connection Path
const myServerPath = String("xxxx\\WS1SQLEXPRESS");
// Connection String Parameter
const config = {
// User Login Details - Windows Auth or General User Account
user : 'xxxx-xxx\\AdrianH',
password: 'xxxxxx',
// Server path to connect to
server : myServerPath,
// Database
datebase: 'plex',
options : {
trustedConnection: true
}
};
sql.connect(config,function(err) {
if (err) console.log(err);
// Create Request Object
var request = new sql.Request();
// Query the Database
request.query('USE plex; SELECT * FROM [plex].[dbo].[tblWorkCenters]',function(err,recordset) {
if (err) console.log(err)
// send records as response
res.send(recordset);
});
});
});
// Start Server and listen on //http://localhost:8001/
var server = app.listen(3213,function(){
console.log('Server is running...');
});
I have hid sensitive information, here is the error code
{ ConnectionError: Login failed for user ''.
at Connection.tedious.once.err (C:\Users\adrianh\node_modules\mssql\lib\tedious.js:244:17)
at Object.onceWrapper (events.js:286:20)
at Connection.emit (events.js:198:13)
at Connection.processLogin7Response (C:\Users\adrianh\node_modules\tedious\lib\connection.js:1397:14)
at Connection.message (C:\Users\adrianh\node_modules\tedious\lib\connection.js:1932:14)
at Connection.dispatchEvent (C:\Users\adrianh\node_modules\tedious\lib\connection.js:1084:36)
at MessageIO.messageIo.on (C:\Users\adrianh\node_modules\tedious\lib\connection.js:984:14)
at MessageIO.emit (events.js:198:13)
at Message.message.on (C:\Users\adrianh\node_modules\tedious\lib\message-io.js:32:14)
at Message.emit (events.js:203:15)
code: 'ELOGIN',
originalError:
{ ConnectionError: Login failed for user ''.
at ConnectionError (C:\Users\adrianh\node_modules\tedious\lib\errors.js:13:12)
at Parser.tokenStreamParser.on.token (C:\Users\adrianh\node_modules\tedious\lib\connection.js:735:29)
at Parser.emit (events.js:198:13)
at Parser.parser.on.token (C:\Users\adrianh\node_modules\tedious\lib\token\token-stream-parser.js:27:14)
at Parser.emit (events.js:198:13)
at addChunk (C:\Users\adrianh\node_modules\readable-stream\lib\_stream_readable.js:297:12)
at readableAddChunk (C:\Users\adrianh\node_modules\readable-stream\lib\_stream_readable.js:279:11)
at Parser.Readable.push (C:\Users\adrianh\node_modules\readable-stream\lib\_stream_readable.js:240:10)
at Parser.Transform.push (C:\Users\adrianh\node_modules\readable-stream\lib\_stream_transform.js:139:32)
at doneParsing (C:\Users\adrianh\node_modules\tedious\lib\token\stream-parser.js:80:14) message: 'Login failed for user \'\'.', code: 'ELOGIN' },
name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
at Request._query (C:\Users\adrianh\node_modules\mssql\lib\base.js:1399:37)
at Request._query (C:\Users\adrianh\node_modules\mssql\lib\tedious.js:546:11)
at Request.query (C:\Users\adrianh\node_modules\mssql\lib\base.js:1335:12)
at C:\Users\adrianh\Desktop\JEC_Current_Projects\WebService\WCWebServiceIOS.js:30:13
at _poolCreate.then.catch.err (C:\Users\adrianh\node_modules\mssql\lib\base.js:287:7)
at process._tickCallback (internal/process/next_tick.js:68:7) code: 'ECONNCLOSED', name: 'ConnectionError' }
** An interesting note to make is in --
(C:\Users\adrianh\node_modules\tedious\lib\token\stream-parser.js:80:14) message: 'Login failed for user \'\'.', code: 'ELOGIN' },
name: 'ConnectionError' }
It doesn't actually seem to pass my login information - any help will be greatly appreciated thank you.
If you are trying to connect mssql via windows authentication in node JS use this module.
var mssql = require('mssql/msnodesqlv8
Sample:
var mssql = require('mssql/msnodesqlv8')
var dbConfig = {
server: 'server',
driver: 'msnodesqlv8',
database: 'DBDATA',
port: '1433',
options: {
trustedConnection: true,
debug: {
packet: false,
payload: false,
token: false,
data: false
},
}
};

Shopify Admin API not working on Google hosted server but works on localhost

My Shopify Admin API not working on Google hosted server but works on localhost. I wonder what am I missing.
I got my order data on localhost but when I upload the code to google server it doesn't work.
I am working on a private app for my shop.
no luck trying to make this works.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const Shopify = require('shopify-api-node');
const shopify = new Shopify({
shopName: 'tinyrabbithole',
apiKey: 'your-api-key',
password: 'your-app-password'
// accessToken: ""
});
shopify.on('callLimits', limits => console.log(limits));
// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.getData = functions.https.onRequest((req, res) => {
shopify.order.list({ limit: 5 })
.then( orders => {
console.log(orders);
return res.status(200).send(orders);
})
.catch( err => {
console.log(err);
return res.status(303).send(err);
})
});
{ RequestError: getaddrinfo ENOTFOUND tinyrabbithole.myshopify.com tinyrabbithole.myshopify.com:443
at ClientRequest.req.once.err (/user_code/node_modules/shopify-api-node/node_modules/got/index.js:182:22)
at ClientRequest.g (events.js:292:16)
at emitOne (events.js:101:20)
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:1025:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
name: 'RequestError',
code: 'ENOTFOUND',
host: undefined,
hostname: 'tinyrabbithole.myshopify.com',
method: 'GET',
path: '/admin/orders.json?limit=5',
protocol: 'https:',
url: undefined }
I'll drop this as an answer instead of leaving it in the comments. If you see a getaddrinfo ENOTFOUND error, first make sure you are on the right payment plan. The free tier doesn't allow outgoing API calls.

How to connect Mongo Atlas Database in Lambda function with Mongoose

I am using Serverless framework to deploy my backend to API Gateway and AWS Lambda.
Here is my serverless.yml for this specific lambda function.
It is a cron job that runs every hour.
cron:
handler: handler.transferHandler
events:
- schedule: rate(1 hour)
Now when I test on localhost it works perfectly. But when I deploy to aws I get the following error:
MongoDB connection error. Please make sure MongoDb is running. { MongoError: failed to connect to server [undefined:27017] on first connect [MongoError: getaddrinfo ENOTFOUND undefined undefined:27017]
at Pool.<anonymous> (/var/task/node_modules/mongodb-core/lib/topologies/server.js:336:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (/var/task/node_modules/mongodb-core/lib/connection/pool.js:280:12)
at Connection.g (events.js:292:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (/var/task/node_modules/mongodb-core/lib/connection/connection.js:189:49)
at Socket.g (events.js:292:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at connectErrorNT (net.js:1021:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
name: 'MongoError',
message: 'failed to connect to server [undefined:27017] on first connect [MongoError: getaddrinfo ENOTFOUND undefined undefined:27017]' }
Here is my function in my handler.js:
exports.transferHandler = function transferHandler(event, context) {
context.callbackWaitsForEmptyEventLoop = false;
mongoose.connect(mongoString, {useMongoClient: true});
const db = mongoose.connection;
db.on("error", (err) => {
console.log("MongoDB connection error. Please make sure MongoDb is running.", err);
process.exit();
});
db.once('open', () => {
BookingModel
.find({})
.then((bookings) => {
...
})
.catch((err) => {
console.log(err);
})
.finally(() => {
db.close();
});
});
};
After 2 weeks of being stuck on this, I learned that I needed to specify my environment variables in my serverless.yml for it to work. My functions were never connecting to the database because my mongo string was never set in lambda. Hope this helps someone.
provider:
name: aws
runtime: nodejs6.10
stage: production
region: us-west-1
environment:
MONGODB_URL: ${env:MONGODB_URL}
S3_BUCKET: ${env:S3_BUCKET}
S3_BUCKET_REPORT: ${env:S3_BUCKET_REPORT}
STRIPE_CLIENT_ID: ${env:STRIPE_CLIENT_ID}
STRIPE_PUBLIC_KEY: ${env:STRIPE_PUBLIC_KEY}
STRIPE_SECERET_KEY: ${env:STRIPE_SECERET_KEY}
JWT_SECRET: ${env:JWT_SECRET}
SEND_BIRD_TOKEN: ${env:SEND_BIRD_TOKEN}
SEND_BIRD_APP_ID: ${env:SEND_BIRD_APP_ID}
MANDRILL_APIKEY: ${env:MANDRILL_APIKEY}

Categories