Mongoose can't connect: Error: querySrv ENOTIMP mongodb.tcp.cluster1-owfxv.mongodb.net - node.js

Mongoose can't connect to mogodb Atlas. It every times give me this error:
Error: querySrv ENOTIMP _mongodb._tcp.cluster1-owfxv.mongodb.net
I am running inside kubernetes cluster inside minikube locally.
If I run project directly then it works perfectly but with minikube it alwasy give me error.
Following is my code:
const url = "mongodb+srv://name:password#cluster1-owfxv.mongodb.net/test?retryWrites=true";
const mongoDbOptions = {
useNewUrlParser: true,
reconnectTries: 10,
autoReconnect: true
};
mongoose.connect(url, mongoDbOptions).then((r) => { }).catch((e) => {
console.log(e);
});
Error message is not so clear to me. Its strange that it works directly but with kubernetes cluster it does not work.
I will really appreciate for any contribution.

Try using connection string compatible with mongo driver 2.2.12 or later i.e. one with mongodb://username:password#host1:port,host2:port,host3:port/databaseName
It's not clear why connection to mongodb is not working with new url.

Using older url, as stated by #cEeNikC, even works when mongoose is giving the following error-
Error: querySrv ETIMEOUT _mongodb._tcp.cluster0.aq9un.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:203:19) {
errno: 'ETIMEOUT',
code: 'ETIMEOUT',
syscall: 'querySrv',
hostname: '_mongodb._tcp.cluster0.aq9un.mongodb.net'
}

go to your cluster setting and enable access from all IP addresses (if you doing this for a learning purpose because it's not safe) .

I add this "&w=majority" and worked:
Before: mongodb+srv://:#sdq25.lepde.mongodb.net/myFirstDatabase?retryWrites=true
After: mongodb+srv://:#sdq25.lepde.mongodb.net/myFirstDatabase?retryWrites=true&w=majority

Related

Redis sentinel connection is timing out from nodeJS

Am trying to connect redis sentinel instance from nodeJS using ioredis. Am not able to connect redis sentinel instance despite trying multiple available options. We have not configured sentinel password. But, able to connect same redis sentinel instance from .net core using StackExchange.Redis. Please find below nodeJS code,
import { AppModule } from './app.module';
import IORedis from 'ioredis';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const ioredis = new IORedis({
sentinels: [
{ host: 'sentinel-host-1' },
{ host: 'sentinel-host-2' },
{ host: 'sentinel-host-3' },
],
name: 'mastername',
password: 'password',
showFriendlyErrorStack: true,
});
try {
ioredis.set('foo', 'bar');
} catch (exception) {
console.log(exception);
}
await app.listen(3000);
}
bootstrap();
Error we got is,
[ioredis] Unhandled error event: Error: connect ETIMEDOUT
node_modules\ioredis\built\redis\index.js:317:37)
at Object.onceWrapper (node:events:475:28)
at Socket.emit (node:events:369:20)
at Socket._onTimeout (node:net:481:8)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
Connection String used from .net core is below,
Redis_Configuration = "host-1,host-2,host-3,serviceName=mastername,password=password,abortConnect=False,connectTimeout=1000,responseTimeout=1000";
Answering this for the benefit of others. Everything is fine, but this nodeJS package is resolving redis instances into private IPs which i cannot access from my local. So, had to put it over subnet group and make it work. However, FYI - .net core package does not resolve into private IPs, hence i was able to access instances from my local itself.
"The arguments passed to the constructor are different from the ones you use to connect to a single node"
Try to replace password with sentinelPassword.

Mongodb Client Connection TypeError NULL issue

Working on a simple nodejs express app using Mongodb. I am getting a typeerror cannot read from null when I try to work with the connection object that is returned. The connection object isn't null so I am not clear on what the error message is actually trying to point out.
the line of code that is generating the error is:
const silos = conn.db('toolkit').collection('silos')
I have a debug console.log right before that of:
console.log("Connection: ",conn)
I am checking for an error on the connect callback and the error is null. Prior to this issue I had an issue with the connection and username/authentication so I know the error checking works as before it was triggering on bad logins.
The error is:
TypeError: Cannot read property 'db' of null
at mongoClient.connect (/var/projects/drake/Routes/Silos.js:22:28)
at err (/var/projects/drake/node_modules/mongodb/lib/utils.js:415:14)
at executeCallback (/var/projects/drake/node_modules/mongodb/lib/utils.js:404:25)
at executeOperation (/var/projects/drake/node_modules/mongodb/lib/utils.js:422:7)
at MongoClient.connect (/var/projects/drake/node_modules/mongodb/lib/mongo_client.js:168:10)
at getSilos (/var/projects/drake/Routes/Silos.js:18:17)
at Layer.handle [as handle_request] (/var/projects/drake/node_modules/express/lib/router/layer.js:95:5)
at next (/var/projects/drake/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/projects/drake/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/projects/drake/node_modules/express/lib/router/layer.js:95:5)
the console.log line generators this message:
Connection: MongoClient {
Here is the code that I use for the Mongo connection. This is in a file and the MongoClient object is exported that is used in other files to connect to the database.
const url = `mongodb://${process.env.MONGO_HOST}:${process.env.MONGO_PORT}`
const opts = {
useNewUrlParser: true,
authMechanism: process.env.MONGO_MECH,
authSource: process.env.MONGO_SRC,
auth: {
user: process.env.MONGO_USER,
password: process.env.MONGO_PWD
}
}
const mongoClient = require('mongodb').MongoClient;
const objectID = require('mongodb').ObjectID
const mongo = new mongoClient(url,opts)
module.exports.mongoClient = mongo
Here is the where I pull in that code and call the connect.
Importing the code:
const { mongoClient } = require('../mongo')
Using the imported code:
mongoClient.connect( (err, conn) => {
if (err) err(res,err, "Database Connection Error", 500)
console.log("Connection: ",conn)
const silos = conn.db('toolkit').collection('silos')
this last line is the one that gives the error.
Results of the console.log
Connection: MongoClient {
One common cause of this is when you try to interact with the mongodb server before a connection is actually made. I might be wrong but I suspect you might be calling your db operations in the wrong order. To avoid this, make sure you have actually successfully connected to the mongodb server before running queries on the collection. If you are using mongoose, then
mongoose.connect('/a/valid/mongo/uri')
will make
mongoose.connection
a valid object that can be interacted with.
Could you please update your question with your connection handling code?
No idea what this was about.
I appreciate the feedback that I got to this.
I spent a weekend rewriting the code from scratch and it worked this time. I did the same thing I believe. There must of been a typo that I was overlooking.
For what it's worth I want to share my experiences with this problem. I was using Mongoose to make the connection but it would crash on either a "replica set" or an "auth error". Eventually I decided to move to MongoClient, just like you did. This is because "Mongoose will not throw any errors by default if you use a model without connecting.".
Moving to Mongo the connection error also returned 'null'. I searched and searched and tried several things, including copying your code and adding the 'auth' option, etc. I eventually switched back to Mongoose (though that shouldn't matter for what solved it for me) and did this:
Create a new user with ReadWrite abilities (didn't help immediately but is definitely needed, since it gave an error when deploying to Heroku without the correct 'new' user.). I now have two users in my MongoDB account.
Add the following to my (Mongoose) Schema:
///Already had this:
let testSchema = new Schema {
foo: String,
bar: Number
}
///Added:
,{
bufferCommands: false,
autoCreate: false
});
Since the DB was still empty I added the following code after creating the model.
///Already had this:
let Test = mongoose.model('Test', testSchema);
///Added:
Test.createCollection();
I believe it then initiated the collection 'for real'. In the mongo shell I know that a collection is not visible until there is an item in there. Now that the app.js didn't crash upon starting I managed to add a document to the database using a form in my HTML and it worked. I now have a working DB connected to my app. Hope this could help anyone also landing on this page.

Node: Can't bind to IPv6 localAddress while using https.request()

I can bind to localAddress just fine when using HTTP, but as soon as I switch to HTTPS I get an error: bind EINVAL. Please consider this code:
var http = require('http');
var https = require('https');
var options = { host:'icanhazip.com',path:'/',localAddress:'2604:a880:1:20::27:a00f',family:6 };
callback = function(response) {
var data = '';
response.on('data',function(chunk) { data+= chunk; });
response.on('error',function(error) { console.log("error: "+error.message); });
response.on('end',function() {
console.log(data);
});
}
http.request(options,callback).end(); // Works. IP:2604:a880:1:20::27:a00f
https.request(options,callback).end(); // Fails. IP:2604:a880:1:20::27:a00f
https.request({host:'icanhazip.com',path:'/',family:6},callback).end(); // Works. IP:2604:a880:1:20::27:a00f
Here's the error while running node v5.0.0:
Error: bind EINVAL 2604:a880:1:20::27:a00f
at Object.exports._errnoException (util.js:860:11)
at exports._exceptionWithHostPort (util.js:883:20)
at connect (net.js:809:16)
at net.js:984:7
at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:63:16)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:82:10)
The only difference between the working and the failing code is setting localAddress and ironically, the last example binds to the correct IP address, but won't let you do it using localAddress.
The problem here is I have to make a request from a completely separate IPv6 address under my use case, and it works fine with HTTP but I need this to work for HTTPS requests. Currently I can only make this work while using cURL. Could you please provide some insight as to why this is happening or how I could make this work without additional libraries?
I had same issue as you. Figured it out. Update your node to the latest stable. They fixed it. Check it with node --version I'm on 6.6.0 and it works great.
The version I got from doing an apt-get was way too old.

intern 2 configuration for BrowserStack behind a proxy

I'm unsure what the configuration should be running intern 2 tests against BrowserStack when running behind a proxy/firewall and currently seeing errors/timeouts.
My Current configuration is:
proxyPort : 9000,
proxyUrl : 'http://localhost:9000',
tunnel : 'BrowserStackLocal',
tunnelOptions : {
username : 'myusername',
accessKey : 'myaccesskey',
hostname : '<myproxyip>',
proxy : 'http://<myproxyip>:<myproxyport>'
}
I don't think I need hostname however if I remove that I immediately see the error getaddrinfo ENOTFOUND.
If use the above configuration it seems to get further, waits for a while then see:
Listening on 0.0.0.0:9000
Starting tunnel...
BrowserStackLocal v3.3
Connecting to BrowserStack using WebSocket protocol...
Connected.
Ready
Error: [POST http://(redacted)#<myproxyip>:4444/wd/hub/session] connect ETIMEDOUT
Error: connect ETIMEDOUT
at errnoException <net.js:904:11>
at Object.afterConnect [as oncomplete] <net.js:895:19>
FATAL ERROR
Error: [POST http://(redacted)#<myproxyip>:4444/wd/hub/session] connect ETIMEDOUT
Error: connect ETIMEDOUT
at errnoException <net.js:904:11>
The [POST http://(redacted)#<myproxyip>:4444/wd/hub/session] url doesn't seem right. Obviously I have this misconfigured & would appreciate any advice. I do struggle to understand intern's documentation when running behind a proxy.
It looks like you are behind some kind of proxy.
When you are behind proxy server, in order to use Intern with BrowserStack, you need to do two things:
Get Local Testing connection to work with your proxy, which you are doing, and you can see success message.
You have to make sure the node process that runs the Intern test works with your proxy. To enable proxy in a node application, the easiest option is to use global-tunnel npm package. There is a step-by-step guide available here.
Would love to answer any queries you might have! Do drop in a mail at support#browserstack.com
Umang,
BrowserStack Support.
I'm not sure but I think there is a bug in browserStack.js. Some proxy support is missing there I think. When I use configuration browserstack_conf.js as shown below tests run okay but there is an error message when Browserstack tries to publish the test results back to www.browserstack.com
[09:43:06] E/launcher - connect ETIMEDOUT 54.152.200.70:443
[09:43:06] E/launcher - Error: connect ETIMEDOUT 54.152.200.70:443
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
[09:43:06] E/launcher - Process exited with error code 199
I can fix this error with following steps:
npm install -g http-proxy-agent
Edit browserStack.js (located for example in C:\Users\myuser\AppData\Romaing\npm\node_modules\protractor\built\driverProviders\ add a require for this and initialize a agent and add to request.
For example add require top of browserStack.js
var HttpProxyAgent = require('http-proxy-agent');
And change the section with options to
let options = {
hostname: 'www.browserstack.com',
port: 443,
path: '/automate/sessions/' + session.getId() + '.json',
method: 'GET',
headers: headers//,
agent: new HttpProxyAgent('http://myproxy:3128')
};
I start Protractor with configuration below
// browserstack.conf.js
var browserstack = require('browserstack-local');
exports.config = {
specs: ['spec.js'],
webDriverProxy: 'http://myproxy:3128',
browserstackUser: 'myuser',
browserstackKey: '*****',
capabilities: {
project: 'BrowserStack (beyond Proxy)',
browserName: 'Chrome',
browser_version: '50.0',
os: 'OS X',
os_version: 'Yosemite',
'browserstack.local': true,
'browserstack.debug': true
},
// Code to start browserstack local before start of test
beforeLaunch: function(){
console.log("Connecting local");
return new Promise(function(resolve, reject){
exports.bs_local = new browserstack.Local();
exports.bs_local.start({'key': '*****',
'proxyHost': 'myproxy', 'proxyPort': '3128',
'binarypath': 'C:\\Users\\myuser\\Downloads\\BrowserStackLocal-win32\\BrowserStackLocal.exe'},
function(error) {
if (error) return reject(error);
console.log('Connected. Now testing...');
resolve();
});
});
},
// Code to stop browserstack local after end of test
afterLaunch: function(){
return new Promise(function(resolve, reject){
exports.bs_local.stop(resolve);
});
}
};

Neo4j in Azure mobile services

I have been trying to use a Neo4j database as my database in an Azure Mobile service. I was following this tutorial and I cant seem to get it to work. Basically what the tutorial does is:
Creates a VM hosted by Azure, running ubuntu and neo4j.
Creates an Azure mobile service with an SQL table.
Writes an insert script for the SQL table that uses node.js to connect to the Neo4j VM and past the data there.
Unfortunately mine does not seem to work. The data gets posted to the mobile services SQL table but the script to post to the Neo4j server on the VM does not seem to work. I also know that the VM is setup correctly because I can connect to it in the Neo4j web admin page.
Here is the code for the script:
function insert(item, user, request) {
//comment to trigger .js creation
var neo4j = require('neo4j');
var db = new neo4j.GraphDatabase('http://<username>:<password>#http://neo4jmobile.cloudapp.net:7474');
var node = db.createNode({ name: item.name });
node.save(function (err, node) {
if (err) {
console.error('Error saving new node to database:', err);
}
else {
console.log('Node saved to database with id:', node.id);
}
});
request.execute();
}
Does any one have any experience with this? Is there something wrong with my script? With the tutorials approach? Any help would be appreciated.
EDIT: There was a problem with the line: console.err('Error saving new node to database:', err); which had to be replace with console.err('Error saving new node to database:', err);. However I am now getting this error message:
Error saving new node to database: { [Error: connect ETIMEDOUT]
stack: [Getter/Setter],
code: 'ETIMEDOUT',
errno: 'ETIMEDOUT',
syscall: 'connect',
__frame:
{ name: 'GraphDatabase_prototype__getRoot__1',
line: 76,
file: '\\\\10.211.156.195\\volume-0-default\\bf02c8bd8f7589d46ba1\\4906fa4587734dd087df8e641513f602\\site\\wwwroot\\App_Data\\config\\scripts\\node_modules\\neo4j\\lib\\GraphDatabase.js',
prev:
{ name: 'GraphDatabase_prototype_getServices__2',
line: 99,
file: '\\\\10.211.156.195\\volume-0-default\\bf02c8bd8f7589d46ba1\\4906fa4587734dd087df8e641513f602\\site\\wwwroot\\App_Data\\config\\scripts\\node_modules\\neo4j\\lib\\GraphDatabase.js',
prev: [Object],
active: false,
offset: 5,
col: 12 },
active: false,
offset: 5,
col: 12 },
rawStack: [Getter] }
Once again, any help would be appreciated!
Did you enable source control and npm install the neo4j module?
Miranda
Ok I managed to figure it out. The problem was that the tutorial (and documentation for neo4j for node.js) had a typo. The code should have been console.error() rather than console.err().

Resources