Node.js and node-sqlanywhere - No Connection Available - node.js

I have been trying to use node-sqlanywhere to query my Sybase database.
However, I cannot seem to be able to connect to the database.
var sqlanywhere = require('sqlanywhere');
var sqlanywhere_conn = sqlanywhere.createConnection();
var sqlanywhere_conn_params = {
DatabaseFile: 'C:\Users\support\Dropbox\database.db',
AutoStart: 'YES',
UserId: 'user_id',
Password: 'password'
};
sqlanywhere_conn.connect(sqlanywhere_conn_params, function(){
sqlanywhere_conn.prepare('SELECT * FROM some_table WHERE some_column = ?', function (err, stmt){
if (err) throw err;
// do something with the statement
});
});
Whether I am trying to connect to the database using Server attribute or the DatabaseFile attribute, I always get error code -2005 No Connection Available.
That being said, I am able to connect to my database using Interactive SQL by either specifying the server or the database file.
So.. I must be missing something here and I can't figure it out !
Thanks !!

Try doubling up the backslash characters, like DatabaseFile: 'C:\\Users\\support\\Dropbox\\database.db'.
FYI you can also ask SQL Anywhere-specific questions on http://sqlanywhere-forum.sap.com.
Full disclosure: I work for SAP in SQL Anywhere engineering. I am responsible for both the node.js driver and the sqlanywhere-forum.sap.com site.

Related

Render Hosted Postgres database throws ECONNRESET on any connection attempt Node Express App

I am unable to query my Postgres DB in Render. I researched this issue online and found that it was a common problem but none of the solutions I found worked.
Here is my server-side code (I am using NodeJS, Express, Typescript and Postgres)
import postgres, { RowList, Row } from 'postgres'
import appconfig from '../app.config'
type Query = (sql: string) => Promise<RowList<Row[]>>
const query: Query = async (sql) => {
try {
const q = postgres({
host: appconfig.database.host,
port: appconfig.database.port,
database: appconfig.database.schema,
username: appconfig.database.username,
password: appconfig.database.password,
})
const res = await q`${sql}`
return res
} catch (err) {
throw err
}
}
export default query
I receive the following error every time and have not had a successful attempt. It's worth noting I have no issues connecting from PGAdmin on the same PC with the same credentials
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:217:20)
at cachedError (C:\Users\xxxxx\repos\one-watch\node_modules\postgres\cjs\src\query.js:171:23)
at new Query (C:\Users\xxxxx\repos\one-watch\node_modules\postgres\cjs\src\query.js:36:24)
at sql (C:\Users\xxxxx\repos\one-watch\node_modules\postgres\cjs\src\index.js:111:11)
at C:\Users\xxxxx\repos\one-watch\src\database\query.ts:15:24
I have never used postgres before, all of my database experience has been in mysql up to this point, although I don't expect this is a postgres problem but potentially just a nuance of Render's implementation of their Postgres service. Any help would be greatly appreciated, thanks!
The only articles I've found like this one are related but they are able to get at least some sort of successful connection at least once. In my case they are all failing.
Adding
?sslmode=no-verify
in the end of the url worked for me.

MongoError - Authentication error (password with #)

Here is my code in my NodeJS application, to connect to my MongoDB engine :
const collection = 'mynewcollection';
const password = 'passwordwithan#';
const mongoUrl = `mongodb://admin:${encodeURIComponent(password)}#mymongobase.net/${collection}`;
// Connect using the connection string
MongoClient.connect(mongoUrl, {useNewUrlParser: true}, function(err, db) {
console.log(err.toString())
});
I get an authentication error. I tried several things to handle the '#' character and reading the documentation I thought that it was the good one...
But it is still failing even if the user and password are the good one.
Is the API correctly used ? Do you understand what is wrong ?
Thanks in advance.
OK I found the solution.
If you use :
const mongoUrl = `mongodb://admin:${encodeURIComponent(password)}#mymongobase.net/${collection}`;
Mongo will try to connect with admin/password defined on the collection.
If you don't put the collection in the mongo url :
const mongoUrl = `mongodb://admin:${encodeURIComponent(password)}#mymongobase.net`;
Then you can use the super user defined on all the mongo engine.

Connection Error to Firebird DB

I have attached my code and error message on the below. Can you please help me, I could not find the reason to get the error.
Thanks,
// My Code
// Node-Firebird
var Firebird = require('node-firebird');
// Options
var options = {};
//options.host = '127.0.0.1';
//options.port = 3050;
options.database = 'mydb.FDB';
options.user = 'SYSDBA';
options.password = 'masterkey';
// Query
Firebird.attach(options, function(err, db) {
if (err)
throw err;
// db = DATABASE
db.query('SOME QUERY', function(err, result) {
// IMPORTANT: close the connection
db.detach();
});
});
// Error Message
/Users/bla/myfile.js:14 throw err; ^ Error: I/O
error during "open" operation for file "/Users/bla/mydb.FDB", Error
while trying to open file at doCallback
(/Users/bla/node_modules/node-firebird/lib/index.js:1233:18) at
/Users/bla/node_modules/node-firebird/lib/index.js:2897:21 at
/Users/bla/node_modules/node-firebird/lib/messages.js:151:25 at search
(/Users/bla/node_modules/node-firebird/lib/messages.js:117:13) at
/Users/bla/node_modules/node-firebird/lib/messages.js:54:21 at
FSReqWrap.wrapper as oncomplete
NOTE:
Actually, I can connect the same database with c++ based driver:
var fb = require("firebird");
var con = fb.createConnection();
con.connectSync('mydb.FDB', 'SYSDBA', 'masterkey', '');
var rs = con.querySync('SOME QUERY');
And when I am trying to connect via Flamerobin, it works perfectly as well. This is really weird error I guess. Any other suggestions, please?
I don't know the node-firebird driver, but given the behavior, one might be connecting locally (with the client library acting as a server), while the other connects through the server. This could potentially lead to the following problems:
Different path resolution as you are specifying a relative path (unless mydb.FDB is defined as an alias), possibly the file /Users/bla/mydb.FDB doesn't exist
Insufficient access rights, the path /Users/bla/mydb.FDB in the error suggests it is a database in a user folder which means that it is not accessible to the Firebird server process (which usually runs under the user firebird).

Can't find Db in mongodb while using mongoose

The following code, works exactly fine giving the output .
var mongoose=require('mongoose');
var dbUrl='mongodb://localhost:27017/trial1';
mongoose.connect(dbUrl);
//Creating schema
var userSchema=new mongoose.Schema({
name:String,
email:String,
createdOn:Date
});
mongoose.model('User',userSchema);
var User=mongoose.model('User');
var userOne=new User({name:'Mike'});
console.log(userOne.name);
mongoose.connection.on('connected',function(){
console.log('mongoose connected to '+dbUrl);
});
mongoose.connection.close(function(){
console.log('connection closed!!!!');
});
But when I try to search for the db in the connection string (ie)trial1, I am not able to find it, The screen shot is as follows.
Also when I say "use trial1" in mongo shell I'm getting the output as per the following screen shot. Which means the db exists or its been created.
Why am I not able to see that db??
So yes the answer is in the comment of Blakes and also part of answer from Pio.
You don't see the databases because your Mongoose code does not actually create any user in it. When you instantiate your Model User it will create it in memory but not in the database. To insert your user in the database you must call the method save on your instance like this:
var userOne=new User({name:'Mike'});
userOne.save(function(err, user){
if (!err) {
console.log('Now my user is saved in the database');
}
})
So if you don't save your user, then in the mongo shell you won't see the databases as it is empty and so does not exists.
what user you use to access the db, maybe the user have no auth on the db.
or, your db url should be this: mongodb://username:password#localhost:27017/trial1.
thanks.
To display a database with show dbs you need to insert at least one document into one of the db's collection. See more details here.

Deploying Node/Mongo to Openshift

Hello I'm trying to get Node/Mongo service going on Openshift, here's what it looks like:
var db = new mongodb.Db('myServiceName',
new mongodb.Server('mongodb://$OPENSHIFT_MONGODB_DB_HOST','$OPENSHIFT_MONGODB_DB_PORT', {}));
db.open(function (err, db_p) {
if (err) { throw err; }
db.authenticate('$USER', '$PASS', function (err, replies) {
if (err) { throw err; }
// should be connected and authenticated.
// ...
The app was created using rhc:
$ rhc create-app myServiceName nodejs-0.10 mongodb-2.4
The console shows the app was started and is running, and on cURL the response is 503
My logs don't show an error, however, the dB is obviously not live. Can anyone help?
If your mongodb driver supports connection with username/password, then use OPENSHIFT_MONGODB_DB_URL instead of OPENSHIFT_MONGODB_DB_HOST
OPENSHIFT_MONGODB_DB_URL gives you this format:
mongodb://admin:password#127.4.99.1:27017/
and OPENSHIFT_MONGODB_DB_HOST gives you this format:
ip addres, ex: 127.4.99.1
So you can just use OPENSHIFT_MONGODB_DB_URL to connect and authenticate at the same time
with mongoskin, you can just do this:
var db = require('mongoskin').db(process.env.OPENSHIFT_MONGODB_DB_URL + 'dbname'+ '?auto_reconnect=true',
{safe: true, strict: false}
);
It looks like you are attempting to connect to a server named "$OPENSHIFT_MONGODB_DB_HOST", (not a valid URL).
Instead, you'll probably want to read the value of the OPENSHIFT_MONGODB_DB_HOST environment variable to find your connection information:
process.env.OPENSHIFT_MONGODB_DB_HOST
I have some additional notes up here: https://www.openshift.com/blogs/getting-started-with-mongodb-on-nodejs-on-openshift

Resources