I am trying to connect to sql server using node js. I have installed the package mssql and using sublime text editor.
this is my node js code.
var sql = require('mssql');
var dbConfig={
server:'CCI-LPT-21',
userName: 'sa',
password: 'sa123#',
port:1433,
options: {
instanceName: 'MSSQLSERVER2K12',
database: 'HRApp',
debug: {
packet: false,
payload: false,
token: false,
data: false
},
}
};
function getEmp()
{
//console.log(dbConfig);
var conn = new sql.Connection(dbConfig);
var req = new sql.Request(conn);
//console.log(conn)
conn.connect(function(err){
if(err){console.log(err);
return;
}
req.query("select * from Team",function(err,recordset){
if(err){
console.log(err);
return;
}
else
{
console.log(recordset);
}
conn.close();
})
})
}
getEmp();
when I run this from command promt,I get this error
when I did Console.log(dbconfig) it was having this data
Your error it's you have to set user and not userName.
Check this example from the doc:
var config = {
user: '...',
password: '...',
server: 'localhost', // You can use 'localhost\\instance' to connect to named instance
database: '...',
options: {
encrypt: true // Use this if you're on Windows Azure
}
}
It's user and not userName.
Related
I'm trying to connect my electron app to a database, but I keep getting the same error
ETIMEOUT
or that the user don't have access
But if I try with my local database works fine, any ideas of why is this happening?
var mysql = require('mysql');
// Add the credentials to access your database
var connection = mysql.createConnection({
host : 'xxx.xxx.xxx',
user : 'xxxxx',
password : 'xxxxxxx',
database : 'xxxxx',
pool: {
max: 1000,
min: 0,
idleTimeoutMillis: 30000000
},
options: {
"enableArithAbort": false,
"encrypt": false
},
debug: true
});
// connect to mysql
connection.connect(function(err) {
if(err){
console.log(err.code);
console.log(err.fatal);
} else {
console.log("OK");
}
});
I am trying to connect to MSSQL database via Windows Authentication in Electron Application.
I use packages:
msnodesqlv8 2.1.0
mssql 7.0.0
As a result, when the pool is connected, the following is displayed:
Error occurred in handler for 'SqlQuery': ConnectionError: Failed to connect to : undefined - self signed certificate
My config code:
import { IResult, config } from "mssql";
const connectionSettings: config = {
server: "my_server_name",
port: 1443,
driver: "msnodesqlv8",
options: {
trustedConnection: true,
connectionString: "Data Source=my_server_name;database=my_database;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
}
};
My connection code:
import { ConnectionPool, IResult, config } from "mssql";
public SqlQuery(poolOptions: config, query: string)
: Promise<IResult<any>> {
return new Promise((resolve, reject) => {
console.log(poolOptions);
const pool = new ConnectionPool({
...poolOptions
});
pool.connect()
.then(() => {
console.log("CONNECT");
return pool.query(query);
})
.then(values => {
console.log(values);
return resolve(values);
})
.catch(reason => {
reject(reason);
});
});
I also face the same error while connecting with SQL server using nodejs
First you have to install mssql,stream and tedious dependency in your project and then write the following code `
var sql = require("mssql");
var config = {
user: "USER_NAME",
password: "PASSWORD",
server: "Your_SERVER_NAME",
database: "DATABASE_NAME",
port: 1433,
options: {
encrypt: false,
useUTC: true,
},
pool: {
max: 100,
min: 0,
idleTimeoutMillis: 3600000,
connectionTimeout: 3600000,
requestTimeout: 3600000,
},
};
var database = new sql.ConnectionPool(config);
database
.connect()
.then(function () {
console.log("connected");
})
.catch((err) => {
console.log(err);
});`
const sql = require("mssql");
var sqlconfig = {
server:"192.168.200.5",
database:"DATA",
user: "labo",
password: "*********",
connectionTimeout: 30000,
port: 1433,
dialect: "mssql",
dialectOptions: {
"instanceName": "SQLEXPRESS"
},
};
new sql.ConnectionPool(sqlconfig).connect().then(pool => {
console.log(pool); return pool.query('select * from data where id = 2')
}).then(result=> {
console.dir(result)
}).catch(err => {
console.log(err);
})
Problem: Return Login failed for user 'Labo' ELOGIN when trying to login to Microsoft SQL Server using node.js.
What I have tried: Grab the example codes on npm to find the avoid extra problems. Error still persisted.
What I suspect: The username I inserted was "labo" but the one returned from err was 'Labo' (with capitalized 'L'). Is this a bug?
This is my code
I have the following two files running on an Express Node.js server:
home.js
var express = require('express')
var sequelize = require('sequelize')
var db = require('../../shared/db.js')
var op = sequelize.Op
var router = express.Router()
router.get('/home', function(req, res, next) {
db.shared.person.findAll({
where: {
email: {
[op.ne]: null
}
},
order: ['id']
}).then(function (person) {
res.locals = {
person: person
}
res.render('home')
})
})
module.exports = router
db.js
var sequelize = require('sequelize')
var config = {
host: 'localhost',
port: 5432,
username: '...',
password: '...',
database: 'postgres',
dialect: 'postgres',
operatorsAliases: false
}
var db = new sequelize(config)
module.exports = {
shared: {
person: db.define('person', {
id: {
type: sequelize.INTEGER,
primaryKey: true
},
name: sequelize.STRING,
email: sequelize.INTEGER
}, { freezeTableName: true , timestamps: false, schema: 'shared' }),
}
}
When I try to run this query, I get an error claiming Unhandled rejection Error: Invalid value { [Symbol(ne)]: null }
What am I doing wrong? I can use $ne and even ne just fine but they've been deprecated and are not entirely safe to use. Furthermore, it's not just [op.ne] - I get this error when I use any conditional like this.
I'm basing this all on this guide so I'm not really sure what I could be doing wrong here.
Unhandled rejection Error: Invalid value might also appear if you didn't setup string aliases like this:
const Op = Sequelize.Op;
const operatorsAliases = {
$eq: Op.eq,
$ne: Op.ne,
...
$any: Op.any,
$all: Op.all,
$values: Op.values,
$col: Op.col
};
const connection = new Sequelize(db, user, pass, { operatorsAliases });
But, better to remove String based aliases from code and use [Op.ne] for example, Sequlize is planning to deprecate them soon.
Sequelize instance in both db.js and home.js are different, this is because node caches a required module based on it path.
To solve this issue you can pass around correct instance in db.js
module.exports = {
shared: {
person: db.define('person', {
id: {
type: sequelize.INTEGER,
primaryKey: true
},
name: sequelize.STRING,
email: sequelize.INTEGER
}, { freezeTableName: true , timestamps: false, schema: 'shared' }),
},
db: db
}
Then finally use operators from that shared instance to do query
var express = require('express')
var sequelize = require('sequelize')
var db = require('../../shared/db.js')
var op = db.db.Op;
var router = express.Router()
router.get('/home', function(req, res, next) {
db.shared.person.findAll({
where: {
email: {
[op.ne]: null
}
},
order: ['id']
}).then(function (person) {
res.locals = {
person: person
}
res.render('home')
})
})
module.exports = router
One more thing, string operators are completely safe to use if you properly sanitize your user inputs. You only need to use secure operators if you are passing un-sanitized user input to Sequelize methods.
More on this topic
http://docs.sequelizejs.com/manual/tutorial/querying.html#operators-security
https://github.com/sequelize/sequelize/issues/8417
I'm using the ftps module and I've got lftp installed on Cygwin. I'm having trouble because my node js app looks like it's connecting fine but none of my commands are running. The documentation for the module isn't very detailed so I've just been trying what ever I can to get this running. I'm tying to get a file from the ftp site.
Here is my code:
var ftps = require('ftps');
// ftps connection
var ftp = new ftps ({
host: 'test.ftpsite.com',
username: 'test',
password: 'test',
protocol: 'sftp'
});
// look at remote directory
console.log(ftp);
ftp.cd('TestDir/').get('/UploadTest.txt', '/cygdrive/c/Users/Administrator/UploadTest.txt').exec(console.log);
Output:
CMO-Application-Server>node app.js
{ options:
{ host: 'test.ftpsite.com',
username: 'test',
password: 'test' },
cmds: [] }
At this point in the output, the app just hangs up like it's attempting to run the commands. I've been letting it run for about 10 minutes now and still nothing.
For sftp, here's how you could do it with the ssh2 module:
var Connection = require('ssh2');
var ssh = new Connection();
ssh.on('ready', function() {
ssh.sftp(function(err, sftp) {
if (err) throw err;
sftp.fastGet('TestDir/UploadTest.txt',
'/cygdrive/c/Users/Administrator/UploadTest.txt',
function(err) {
if (err) throw err;
ssh.end();
});
});
}).connect({
host: 'test.ftpsite.com',
port: 22,
username: 'test',
password: 'test'
});