I have an external dll (that written in c#) which is responsible of connecting to my DB.
I"m using this dll in my node.js application (Javascript).
When connecting to MSS DB connection result is true, when connecting to Oracle db it constantly fails with no option to know what went wrong.
but if im connecting to the Oracle DB thru a test project (written in c#) in my visual studio , the result is true.
the problem accord only when i use the dll thru the nodejs.
How can I resolve this?
This are different databases and need different connection modules.
I would suggest writing a separate connection in node as it is really easy with the node-oracledb module:
var oracledb = require('oracledb');
oracledb.getConnection(
{
user : "hr",
password : "welcome",
connectString : "localhost/XE"
},
function(err, connection)
{
if (err) { console.error(err); return; }
connection.execute(
"SELECT department_id, department_name "
+ "FROM departments "
+ "WHERE department_id < 70 "
+ "ORDER BY department_id",
function(err, result)
{
if (err) { console.error(err); return; }
console.log(result.rows);
});
});
You need to install the module first using npm.
More info here: https://blogs.oracle.com/opal/entry/introducing_node_oracledb_a_node
Related
I have created four postresql tables. I am using nodejs with knexjs as a query builder.
I can create and execute migrations with the command line without any problems. Now I want to run migrations via Javascript. How can I proceed with this?
Here is my code: -
module.exports.runDBMigrations = async () => {
knex.schema.hasTable(USERS_DB_NAME).then(function (exists) {
if (!exists) {
//Execute migrations to create users tables
}
})
.catch(e => {
console.log("Error creating tables: ", e);
})
knex.schema.hasTable(POSTS_DB_NAME).then(function (exists) {
if (!exists) {
//Execute migrations to create posts tables
}
})
.catch(e => {
console.log("Error creating tables: ", e);
})
knex.schema.hasTable(LIKES_DB_NAME).then(function (exists) {
if (!exists) {
//Execute migrations to likes users tables
}
})
.catch(e => {
console.log("Error creating tables: ", e);
})
knex.schema.hasTable(FOLLOWERS_DB_NAME).then(function (exists) {
if (!exists) {
//Execute migrations to create followers tables
}
})
.catch(e => {
console.log("Error creating tables: ", e);
})
}
Hi,
Couple days I was sorrounding about how to run migrations and seeds when Server is starting.
Doing some researchs adn reading the Knex documentation, this is the solution what I reach to. Maybe this can help you.
Stack
lib
version
nodejs
16.17.x
nodemon
^2.0.19
npm
8.19.x
knex
^2.3.0
express
^4.18.1
pg
^8.8.0
postgreSQL
14.5
typescript
^4.8.2
Solution
1. Create a void function runMigrations(). (I located in models/setup/) (you can name it as how you want)
This function will have the knex singleton calling for running the migrations and the seeds.
2. Runn all the pending migrations calling the knex migrate .latest() method
This method is a promise, so you can capture the success creation or the error in case something goes wrong.
In this part, you can run all the seeds calling the knex seed .run() method. You can this method in the first callback
Call the migrations method in your index or server file. In my case Is runMigrations() method.
Restart your server, so you can check if everything works fine.
If you go to the terminal you can check response status. I add some log response, in order to know if everything working fine.
Note
I provoque an error so you can check the resutl if something were bad
executing the migrations or seeds
Maybe you'll find or your found others alternative of solution, but if you reach to fix the issue and added a different way; Please share it as a comment, so we can exchange knowledge and see a different way how to solve this issue.
Regards
EDITED:
I am working on a page that takes the information a user enters into a form and adds it to a database. I created the database with mysql. When trying to connect to my database using javascript. My code breaks on var mysql = require('mysql');:
I realized that this may be due to either the code being ran in a function or having the DOM related code in the function...
I tried running a simple version of the code without those elements and it works to connect to and update the database but I need to be able to pull the values from the form so I am still struggling with how to do this.
function saveUserForm() {
var firstName = document.getElementById("firstName");
var lastName = document.getElementById("lastName");
var psw = document.getElementById("psw");
var userName = document.getElementById("userName");
var email = document.getElementById("inputText");
alert('test');
var mysql = require('mysql');
alert("mysql test");
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "abc123",
database: "PBSC_Parking_DB"
});
con.connect(function(err) {
if (err){
throw err;
alert("error");
}
var sql = "INSERT INTO accounts (UserName, FirstName, LastName, Email, UserPassword) VALUES ('"+userName+ "', '"+firstName+"','"+lastName+"','"+email+"','"+psw+"')";
con.query(sql, function (err, result) {
if (err) {
throw err;
alert("test error");
}
alert("account added");
console.log(result.affectedRows + " record(s) updated");
});
});
}
you can try writing npm i mysql in the built in terminal of your ide instead of cmd and check that the mysql folder is added to your project dependencies
or you can run npm i --g mysql to install that globally
require('mysql') is async, so it needs to be treated as a promise. For safety, I would load this outside of the function scope. The only time you would need to require something inside the scope of a function is for lazy loading local assets.
You try to include a library with node from cmd so that's normal to give you undefined.
cause node declares values like this. so you seem to install MySQL with npm globally. so it appears with you.
that's not related to your code at all.
you need to produce bug in your code via log error or debug code!
I am still new to node.js so I apologize in advance if this question is silly.
First I have tried to execute the test.js with command prompt
conn.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'tgs'
});
connection.connect(function(err) {
if (err) throw err;
});
module.exports = connection;
test.js
function testconnect()
{
var conn = require('./conn');
var sql = "INSERT INTO chat_line (line_text) VALUES ('test')";
conn.query(sql, function (err, result) {
if (err) throw err;
});
}
testconnect();
in the command prompt
D:\xampp\htdocs\TGS\>node test.js
and it works, I did get the input in my database. After that I wanted to try to implement this in my system so I have tried to execute this in php
testing.php
<script>
function testalert()
{
alert("alert1");
var conn = require('./conn');
var sql = "INSERT INTO chat_line (line_text) VALUES ('test')";
conn.query(sql, function (err, result) {
if (err) throw err;
});
alert("alert2");
}
</script>
<input type="button" onclick="testalert();">
and it doesn't work. The first alert did pop up after clicking the button but the second alert did not, also there is no input in my database.
Node.js and PHP are different technologies, you cannot directly use one in another if you want to interact with one another you will need some mediator like (API, queue, database etc).
try to understand how nodeJs works, there is a dependency of mysql library at line one, which is a middleware helping with dealing with database
var mysql = require('mysql');
above line can only work in nodeJs environment, if you want to use it in PHP then there will be some other library in PHP to do same (I am not PHP guy so cannot help with ideal one here) and you will not need nodeJS at all with your example.
do let me know if I made it clear.
Dear arangodb community,
How mature is arangojs? When I tried "Tutorial: Node.js (io.js) in 10 minutes", the exercises 1 thru 4 work as expected. But the 5 thru 10 failed. From the following exercise, I am getting
Database created: undefined
instead of
Database created: "mydb"
Thus the remaining exercises cannot continue, since the crucial object-bearing variable (mydb) is null. But, observing that "mydb" data base is correctly created in arangodb, my question is just related to the maturity of aragogojs (arangodb's Javascript driver). Or how do I fix it?
db.createDatabase('mydb', function(err, newdb) {
if (err) {
console.log('Failed to create database: %j',
err.message);
} else {
console.log('Database created: %j', newdb.name);
mydb = newdb;
}
});
Thanks
The announced, updated node tutorial using the 4.x version of arangojs is available now.
Creating a new database changed to:
db.createDatabase('mydb').then(
() => console.log('Database created'),
err => console.error('Failed to create database:', err)
);
All asynchronous methods in the ArangoDB driver return promises but you can also pass a node-style callback instead:
db.createDatabase('mydb', function (err) {
if (!err) console.log('Database created');
else console.error('Failed to create database:', err);
});
The Node tutorial is based on version 3.x of the arangojs driver. The driver has recently been updated to version 4.x, which contains a number of breaking API changes.
The tutorial will soon be updated to reflect these changes. In the meantime you can follow the tutorial by installing version 3 explicitly:
npm install arangojs#3
I am learning node and mongodb
My app is working fine (Its is online tutorial app)
I have two Question
// define model =================
var Todo = mongoose.model('Todo', {
text : String
});
// create todo and send back all todos after creation
app.post('/api/todos', function(req, res) {
// create a todo, information comes from AJAX request from Angular
Todo.create({
text : req.body.text,
done : false
}, function(err, todo) {
if (err)
res.send(err);
// get and return all the todos after you create another
Todo.find(function(err, todos) {
if (err)
res.send(err)
res.json(todos);
});
});
});
I hope you understand my code (Its todo adding app)
1) Is 'Todo' is collection in mongoDB ?
I typed on cmd db.collection.find() but nothing result ? , Which command i use on cmd to see data here
2) Can I open DB data storage file in my editor with any tool ?
Online tutorial app link
Thanks
First you have to select the database.
use database-name
You can find the list of collections in database using the following command.
show collections
You can find the list for databases using the following command
show dbs
Then use the db.collection.find() command.
use help command to find list of commands.