nodeJS connects to oracle
var oracle = require('./');
var connectData = {
hostname: "myhost",
port: 1521,
database: "mydb", // System ID (SID)
user: "usertest",
password: "mypass"
}
oracle.connect(connectData, function(err, connection) {
if (err) { console.log("Error connecting to db:", err); return; }
connection.execute("SELECT * FROM <table_name>", [], function(err, results) {
if (err) { console.log("Error executing query:", err); return; }
console.log(results); //This statement's not working
connection.close(); // call only when query is finished executing
});
});
output-
C:\xampp\htdocs\motcua.dev\public\socket.io\node_modules\oracle>node tests-connection
C:\xampp\htdocs\motcua.dev\public\socket.io\node_modules\oracle>
Question- It is exiting without giving any output please help me with this
Related
Good day, I recently had help learning how to make things wait using callbacks, however now it's almost as if I hit a limit on how many can be used, but I can't seem to find information on this?
If I ran this code without tryToCreateAccount having a callback, it will connect, check and work as intended. As soon as I make tryToCreateAccount have a callback param, it appears to enter Connect.Connect (only sometimes) but then never query's.
Backend:
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'host',
user: 'user',
password: 'pass',
database: 'db'
});
export function tryToCreateAccount(login, password, passwordConfirm, callback)
{
checkAccountNameAvailable(login, connection, function(err, accountNameAvailable)
{
callback(err, accountNameAvailable);
});
}
function checkAccountNameAvailable(login, connection, callback)
{
var accountNameAvailable = true;
console.log("Checking account name", login);
connection.connect(function (err)
{
console.log("Checking Connect Method");
if(err) callback(err);
connection.query("SELECT login FROM accounts WHERE login = ?", login, function (err, result)
{
if (err) callback(err);
if(result.length > 0) accountNameAvailable = false;
console.log("returning ", accountNameAvailable);
callback(null, accountNameAvailable);
connection.end();
});
});
}
Front End:
tryToCreateAccount($w('#input1').value, $w('#input2').value, $w('#input3').value, function (err, callback)
{
if(callback)
{
console.log("Was True");
}
else
{
console.log("Was False");
}
});
UPDATED CODE:
Backend:
const mysql = require('mysql');
const pool = mysql.createPool(
{
connectionLimit : 10,
host : 'host',
user : 'user',
password : 'pass',
database : 'db'
});
export function tryToCreateAccount(login, password, passwordConfirm, callback)
{
checkAccountNameAvailable(login, pool, function(err, accountNameAvailable)
{
if(err)
{
console.log(err);
callback(err, null);
}
callback(err, accountNameAvailable);
});
}
function checkAccountNameAvailable(login, pool, callback)
{
var accountNameAvailable = true;
console.log("Checking account name", login);
pool.getConnection(function (err, connection)
{
console.log("Made a connection.");
if(err)
{
console.log(err);
callback(err, null);
connection.destroy();
}
connection.query("SELECT login FROM accounts WHERE login = ?", login, function (err, result)
{
console.log("Checking Query Method");
if (err)
{
console.log(err);
callback(err, null);
}
if(result.length > 0) accountNameAvailable = false;
console.log("returning ", accountNameAvailable);
callback(null, accountNameAvailable);
connection.destroy();
});
});
}
Frontend is the same
Output:
Checking account name testaccount
I am using Knex query builder and Postgres as my data base.
I need to get the output from a raise notice or raise info from a Postgres function or procedure.
raise notice 'This is my Log';
we need to set notice after pool created, for that we need to use afterCreate function.
for your references
https://github.com/knex/knex/issues/4241
The sample code is below.
var pConn = {
host: 'SERVER',
port: 'PORT',
user: 'USER',
password: 'PASSWORD',
database: 'DATABAENAME'
};
var query = "select * from pglog_test()";
try {
const knex = require('knex')({
client: 'pg',
connection: pConn,
pool: {
afterCreate: function (conn, done) {
conn.query('select 1 as result', function (err, result) {
conn.on('notice', function (msg) {
// here we can get the pg procedure logs (raise notice/raise info)
console.log('logs from postgress' + msg);
});
done(err, conn);
});
}
}
});
// procedure calling
knex.raw(query).then(function (result) {
console.log(result);
}).catch(function (error) {
console.log(error);
});
} catch (error) {
console.log(error);
}
There is a standard way to connect to oracle db.
var oracledb = require('oracledb');
oracledb.getConnection(
{
user : "hr",
password : "mypw",
connectString : "localhost/XEPDB1"
},
function(err, connection) {
if (err) {
console.error(err.message);
return;
}
connection.execute(
`SELECT *
FROM departments`,
[],
{
outFormat: oracledb.OBJECT
},
function(err, result) {
if (err) {
console.error(err.message);
doRelease(connection);
return;
}
console.log(result.rows);
doRelease(connection);
});
});
function doRelease(connection) {
connection.close(
function(err) {
if (err)
console.error(err.message);
});
}
But how to make it so that the connection to the database is passed, then it receives an array that is stored in the database, and after that the connection was terminated. Then, in the future, to make it possible to use the resulting array to send requests for adding data, but for another database.
I am getting error Invalid object name dbo.tbl_user.while running a query on the Azure database from a nodejs app.My code which runs is following.
const pool = new sql.ConnectionPool(DatabaseConfig.config);
pool.connect().then(function () {
//6.
debugger;
var request = new sql.Request(pool);
//7.
request.query("select * from [dbo].[tbl_user]").then(function (recordSet) {
debugger;
console.log(recordSet);
sql.close();
}).catch(function (err) {
//8.
debugger;
console.log(err);
sql.close();
});
}).catch(function (err) {
//9.
debugger;
console.log(err);
});
My connection string is :
config: {
server: 'azureserver',
options: {
database: 'cccc',
encrypt: true,
port: 1433
},
user: 'ccc',
password: 'ccccc'
}
what is the mistake I am doing here? Thanks in advance for the help.
I tested your code on my side but did not reproduce your issue.
Here is my test code:
const sql = require('mssql')
const config = {
server: '***.database.windows.net',
database: '***',
user: '***',
password: '***',
options: {
database: '***',
encrypt: true,
port: 1433
}
}
const pool = new sql.ConnectionPool(config);
pool.connect().then(function () {
//6.
debugger;
var request = new sql.Request(pool);
//7.
request.query("select * from [dbo].[Student]").then(function (recordSet) {
debugger;
console.log(recordSet);
sql.close();
}).catch(function (err) {
//8.
debugger;
console.log(err);
sql.close();
});
}).catch(function (err) {
//9.
debugger;
console.log(err);
});
Query result on the portal:
Run query.js :
You need to make sure the table name is correct, it's not related to spelling case.
Hope it helps you.
I am trying to fetch a table, whose name is defined in another table and return the results.
I have defined a database module with these methods:
var mysql = require("mysql");
var config = require("../config/database.js");
module.exports = {
pool: mysql.createPool({
host: config.host,
user: config.user,
database: config.database,
password: config.password,
}),
getConnection: function(callback) {
this.pool.getConnection(callback);
},
fetch: function(table, callback) {
this.getConnection(function(err, connection) {
if(err) {
callback(err);
} else {
connection.query("SELECT * FROM ??", table, callback);
connection.release();
}
});
},
};
I am trying to fetch the table and then the "sub" table like so:
db.fetch("foo", function(err, rows) {
if(err) {
throw err;
} else {
_.each(rows, function(row) {
db.fetch(row.tbl, function(err, result) { console.log(err, result); });
});
}
});
However the inner-fetch callback is never called.
Why is this? I suspected I needed to close the first connection before establishing a second so I tried taking the each loop outside but encountered things becoming out of sync.