Postgres database does not connect, .sync() does not resolve. Sequelize, PostgreSQL, Node - node.js

Recently, I got a new laptop and I am set up my development environment on there. Also, I copied a project I worked on on my old laptop and wanted to continue working on it on my new laptop. Nothing weird here, I would think. The server-side code in this case.
So I started with installing all the apps and programs, cloned my GitHub repo, booted up a docker container with the following command:
docker run -d -p 5432:5432 --name ulti-mate -e POSTGRES_PASSWORD=secret postgres
and ran npm install. I connected to the database using postico, just to have a little visual feedback, which connected instantly. Then started the server up using nodemon index.js and it seemed to start. Only I was missing one thing: Normally the server console logs Database connected and runs a bunch of insert queries. Went to Postico, nothing.
I've been going over the code of my database:
const Sequelize = require('sequelize');
const databaseUrl =
process.env.DATABASE_URL ||
'postgres://postgres:secret#localhost:5432/postgres';
const db = new Sequelize(databaseUrl);
async function syncDB() {
try {
console.log('Before sync');
await db.sync({ force: false });
console.log('Database connected');
} catch (error) {
console.error('error synching database', error);
}
}
syncDB();
and I noticed that it runs until it hits db.sync(). Before sync logs consistently. That's where it stops. It doesn't resolve at all. I tried assigning it to a variable, but nothing. For example, this does not log:
const a = await db.sync({force: false});
console.log("a:", a);
The weird thing is that it worked before on my old machine, so the problem can't be in the code. It must have something to do with my new development environment. I tried installing different versions of sequelize and pg in the repo, didn't help. I tried reinstalling postgresql with homebrew, but it's up to date.
If anyone has an idea what might be going wrong or something I might try to fix this issue, it would be greatly appreciated, because I'm slowly going mad.

I figured out the problem. I was running Node v14.4.0 (latest version at the moment). Downgrading to the latest LTS version (v12.18.1) fixed the issue. I'm not sure if this is a known issue, but I opened a ticket on the sequelize Repo.

Related

Nodemon server won't start

I was trying to create a simple room based webchat app using socket.io. But I've been facing issues with serving the node server file with node and nodemon. I've tried different ports i.e., 8000, 8080, 80 and I've also tried reinstalling node and nodemon both locally and globally but nothing seems to work. I guess the code is alright because I was following a tutorial (that didn't include any link to the source code though). I'm on windows so I've also used used the command
Set-ExecutionPolicy Unrestricted
Here are the screenshots of Windows power shell and command prompt
PS: I am a newbie, so please consider that I might have missed something basic.
const io = require('socket.io')(8000)
const users = {};
io.on('connection', socket =>{
socket.on('new-user-joined',name =>{
users[socket.id] = name;
socket.broadcast.emit('user-joined', name);
});
socket.on('send', message =>{
socket.broadcast.emit('receive', {message: message,name: user[socket.id]});
});
})

How do I get a response/information from a hanging pgPool.connect()?

I am using the pg package (node.js), and for some reason the connect function gives me nothing. My code gets hung up on that line and I'm unable to see any errors, what's wrong, or what's happening.
i.e.
console.log("HERE");
await pgPool.connect()
console.log("NOW HERE") //this line never prints
I've tried a bunch of variations too:
console.log("HERE");
const client = await pgPool.connect()
console.log(client) //this line never prints
Does anyone know how to get a verbose stream from pg? My pg version is 7.15.0 and my npm version is 6.14.4
I've tried waiting it out for over an hour. For friends running the same code from the same branch on their local machines it connects in under a second. I've confirmed they have the same version of pg as me.
I am able to connect directly to the database using psql in a separate terminal without issues (it immediately connects in < 1 second)
Updated my pg to 8.2.1 and it solved the problem. Must be an incompatibility issue with an earlier version

strapi start: "url is not defined" - using mlab mongo DB

When I run 'strapi start' I get the following output in my terminal:
debug ⛔️ Server wasn't able to start properly.
Error URL is not defined
Any solutions? I have recreated my mlab DB 3 times and when I enter all the values in the terminal to create the new strapi server I double check my values and everything from GitHub issues and strapi documentations say I'm doing the correct thing, but not able to figure out why this is happening! Appreciate any help!
As #soupette have said in the comment:
Strapi needs Node v10 to be installed in order to work. If you upgrade
your node version it should be working properly
I can confirm the same error on my end with NodeJS 8.11.1
Strapi's GitHub page confirms that minimum NodeJS version is 10.x
Ok when you create a Mondo db on mlab you get this information:
mongo ds129484.mlab.com:29484/blog -u <dbuser> -p <dbpassword>
mongodb://<dbuser>:<dbpassword>#ds129484.mlab.com:29484/blog
When you start a strapi project, you make this
strapi new blog
cd blog
strapi start
So you select mondodb how to default db and the cli show the follow questions, and you would be add the following answers, supposing that you have the db data showed before:
DBname: blog
host: ds129484.mlab.com
srv connection: false
port: 29484
username: username created on mlab for your db
password: username password created on mlab for your db
authentication db: blog (you have put the name of your db)
enable ssl: false
You press Enter and you project would be run perfectly
First of all you must have Node 10 or above. If you still getting error edit the following file:
config/host.json
Use an higher timeout value.
{
"timeout": 60000,
......
}
Assuming you are on the correct node version, goto the configuration.js found in node_modules/strapi/lib/core/configurations.js and update lines 331-333.
Replace
this.config.admin.url = this.config.admin.devMode ?
(new URL(adminPath, `http://${this.config.host}:4000`)).toString():
(new URL(adminPath, url)).toString();
With
this.config.admin.url = this.config.admin.devMode ? `http://${this.config.host}:4000/admin`:`http://${this.config.host}:1337/admin`
Thanks
Here is what worked for me:
Just upgrade your node js installation to version 10

Failed Database Connection between OpenShift Server and Mlab.com?

I'm hoping to host a node.js server at OpenShift, utilizing a MongoDB database hosted at mlab.com (the new version of mongolab.com) Here's a pretty straight forward tutorial. According to that tutorial, things seem pretty straight forward. That tutorial may be a bit dated, but seems to have been targeted directly for my application (less the update from Mongolab --> mlab) I've used Mongolab in the past and they provide a great service.
So I've built my database. I've written my node code and tested it from local host, where it works great. Yes there are a few lines of difference, but not much. I'm using the same git directory as I'm pushing to OpenShift. The code is pretty straight forward.
databaseUrl = 'mongodb://UserNameHere:PasswordHere#ds012345.mlab.com:12345/DataBaseName';
if (process.env.MLAB_URI) {
databaseUrl = process.env.MLAB_URI;
}
MongoClient.connect(databaseUrl, function(err, db) {
assert.equal(null, err, "Database Connection Troubles: " + err);
test process.env.MLAB_URI from my terminal after a RHC login.
[ABC-XYZ.rhcloud.com xxxxxxxxxxx]\> echo $MLAB_URI
mongodb://<username>:<password>#ds012345.mlab.com:12345/DataBaseName
[ABC-XYZ.rhcloud.com xxxxxxxxxxx]\> echo $OPENSHIFT_REPO_DIR
/var/lib/openshift/xxxxxxxxxxxx/app-root/runtime/repo/
Test with $, use in code with process.env. call. Obviously I've changed my username, password and Openshift server identification, but I've checked and there appear to be no spelling errors. I get the same fail on openshift if I don't use the MLAB_URI environment variable. It's like the connection from the OpenShift server is shut off.
Mlab does provide some tools to verify the connection to a MongoDB there. Here's link to the Mlab assist stuff. I can ping the mlab location from a RHC login and it works just fine. Unfortunately I'm unable to do the % netcat -w 3 -v ds012345.mlab.com 12345 test. That tool (netcat / nc) isn't available at OpenShift.
Again, this thing works fine when I run my node file.js from my local host. I can see data being deposited at the mlab server. It fails if I run from Openshift, with a
throw err ^
AssertionError: Database Connection Troubles: MongoError: auth failed
The code works fine if I use a MongoDB cartridge in the same gear at OpenShift. Unfortunately I've got a few different servers at different locations that are all sharing information. Anybody know what's going on here?
Update: I've done some additional testing from a terminal with RHC login to OpenShift.
[ABC-XYZ.rhcloud.com xxxxxxxxxxx]\> mongo ds012345.mlab.com:12345/dbName -u <dbuser> -p <password>;
MongoDB shell version: 2.4.9
connecting to: 127.6.xyz.xyz:27017/admin
Fri Mar 11 04:14:52.770 Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" } at src/mongo/shell/db.js:228
exception: login failed
The one surprise is that connecting to: url:27017/admin line... I'd like to understand that better. Stay tuned.
Update for anybody else who may get here. I submitted a support request to mlab. I received an immediate response (Awesome support!)
You'll need to upgrade your mongo shell version to 3.0+ in order to
connect and authenticate to an mLab Sandbox database server. It looks
like version 2.4.9 is being used.
So I was definitely using mongo shell version 3.0 from my localhost. I have little control # OpenShift for that command line feature.. But whoa... Let's not forget the big picture here. I'm really trying to use my node server to contact mlab via a var MongoClient = require('mongodb').MongoClient; connect call. Let's make the same check. Do I have the latest version of mongodb listed in my package.json file? Oops..
Easy fix. Update package.json to require a newer version of mongodb. Success at OpenShift. Yipee!

Nodejs and Sphinx data query issue

I have been using limestone module and Nodejs to query sphinx index. The limestone is out-dated in my npm so i downloaded from github and it is got connected to the sphinx server successfully. But i am now facing the issue as follows,
When i tried to execute the following code,
var limestone = require("limestone").SphinxClient(),
sys = require("sys");
limestone.connect("192.168.2.443:9312", // port. 9312 is standard Sphinx port. also 'host:port' allowed
function(err) {
if (err) {
sys.puts('Connection error: ' + err);
}
sys.puts('Connected, sending query');
limestone.query(
{'query':'raja',maxmatches:1},
function(err, answer) {
if(err){
console.log("Sphinx ERR: "+err);
}else{
console.log(JSON.stringify(answer));
limestone.disconnect();
}
});
});
i got the below error,
Sphinx ERR: Searchd command older than client's version, some options might not workServer issued ERROR: 0bad multi-query count 0 (must be in 1..32 range)
Please help me on this!
Ok, so I installed sphinxseach on Ubuntu, the version in the repository is 0.9.9. I got a similar error as yours:
Searchd command older than client's version, some options might not workServer issued ERROR: Qclient version is higher than daemon version (client is v.1.24, daemon is v.1.22) undefined
After looking through the issues at limestone's github, I figured it was supposed to work with Sphinx version 2. So I installed 2.0.4 from Sphinx download page (they have Ubuntu packages), and it works! So, if it's possible for you to upgrade, that might be a good idea anyway -- and limestone will probably only ever track the latest release.

Resources