Following tutorial to connect postgres db to node project - failing at running script to create table - linux

i am following a guide on how to use postgres + node. I have gotten to the point of creating table creation script.
here is the code I am using from the tutorial:
var pg = require('pg');
var connectionString = process.env.DATABASE_URL || 'postgres://postgres:postgres#localhost:5432/todo';
var client = new pg.Client(connectionString);
client.connect();
var query = client.query('CREATE TABLE items(id SERIAL PRIMARY KEY, text VARCHAR(40) not null, complete BOOLEAN)');
query.on('end', function() { client.end(); });
However, when I do the step of node models/database.js, I receive the following error:
ram#ram-windows-xp-ubuntu:~/Development/Web/Projects/node-postgres$ node models/database.js
events.js:85
throw er; // Unhandled 'error' event
^
error: password authentication failed for user "postgres"
at Connection.parseE (/home/ram/Development/Web/Projects/node-postgres/node_modules/pg/lib/connection.js:539:11)
at Connection.parseMessage (/home/ram/Development/Web/Projects/node-postgres/node_modules/pg/lib/connection.js:366:17)
at Socket.<anonymous> (/home/ram/Development/Web/Projects/node-postgres/node_modules/pg/lib/connection.js:105:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)
I am new to using the ubuntu and I tried to follow the steps from this guide but confused. Sorry for the bother friends please if you can do the needful I will be humbled.

Related

Issue with connecting to rethinkdb from Windows 11 via thinky (nodejs)

I'm trying to onboard a new developer, that is using Windows 11 as the only one on our small team. I've guided him through installing WSL2 and Ubuntu 20.04.3 LTS (linux kernel: 5.10.93.2-microsoft-standard-WSL2).
We are 3 other developers who are using native Ubuntu, WSL2 Ubuntu 21.04 and macOS, respectively.
We are all on nodejs 16.14 with the exact same package-lock.json file.
He is the only one getting an Error [ERR_STREAM_WRITE_AFTER_END]: write after end.
tldr;
Both errors are about writing to a Buffer.
Is anyone aware of any related issues to nodejs's Buffer implementation on Windows 11?
We are using thinky as outlined below:
'use strict';
const createThinky = require('thinky');
const { rethinkdbConfig } = require ('./utils/config.js');
const thinky = createThinky (rethinkdbConfig);
// Thinky is our ORM
var type = thinky.type;
// Creates the thinky DB model for payments - we save the amount and the stripe customerID for each transaction
// You can execute any rethinkdb query language on the model, eg Payment.count().execute()
var Payment = thinky.createModel("payments", {
id: type.string(),
amount: type.number(),
customerID: type.string(),
project: type.string(),
projectName: type.string(),
projectPercentage: type.number(),
firefundPercentage: type.number(),
type: type.string(),
processor: type.string(),
email: type.string(),
charged: type.boolean(),
recharged: type.boolean()
});
module.exports = {
model: Payment,
};
Now, it doesn't matter if he connects to a local instance of rethinkdb (2.4.1~0focal), our staging or production rethinkdb (2.3.5~0trusty) on AWS.
I got him to try Netcat with nc -zv [url] 28015 to see if he could connect at all and he got connectıon successful. So I do not think it is a firewall issue.
Error stack trace
node ./bin/www
firefund:www Listening on port 3000 +0ms
node:events:498
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:371:5)
at _write (node:internal/streams/writable:319:11)
at Socket.Writable.write (node:internal/streams/writable:334:10)
at Connection._sendProof (/home/edel_weiss/firefund-production/node_modules/rethinkdbdash/lib/connection.js:294:19)
at /home/edel_weiss/firefund-production/node_modules/rethinkdbdash/lib/connection.js:248:12
at Object.tryCatch (/home/edel_weiss/firefund-production/node_modules/rethinkdbdash/lib/helper.js:170:3)
at Connection._computeSaltedPassword (/home/edel_weiss/firefund-production/node_modules/rethinkdbdash/lib/connection.js:247:12)
at Socket.<anonymous> (/home/edel_weiss/firefund-production/node_modules/rethinkdbdash/lib/connection.js:184:18)
at Socket.emit (node:events:520:28)
at Socket.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
Emitted 'error' event on Connection instance at:
at Socket.<anonymous> (/home/edel_weiss/firefund-production/node_modules/rethinkdbdash/lib/connection.js:129:12)
at Socket.emit (node:events:520:28)
at Socket.emit (node:domain:475:12)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
The stack trace indicates that it is a connection error in node_modules/rethinkdbdash/lib/connection.js.
connection.js
connection.js: line 294 is this.connection.write(Buffer.concat([new Buffer(message.toString()), NULL_BUFFER])) at the bottom.
Connection.prototype._sendProof = function(authentication, randomNonce, saltedPassword) {
var clientFinalMessageWithoutProof = "c=biws,r=" + randomNonce;
var clientKey = crypto.createHmac("sha256", saltedPassword).update("Client Key").digest()
var storedKey = crypto.createHash("sha256").update(clientKey).digest()
var authMessage =
"n=" + this.user + ",r=" + this.randomString + "," +
authentication + "," +
clientFinalMessageWithoutProof
var clientSignature = crypto.createHmac("sha256", storedKey).update(authMessage).digest()
var clientProof = helper.xorBuffer(clientKey, clientSignature)
var serverKey = crypto.createHmac("sha256", saltedPassword).update("Server Key").digest()
this.serverSignature = crypto.createHmac("sha256", serverKey).update(authMessage).digest()
this.state = 2
var message = JSON.stringify({
authentication: clientFinalMessageWithoutProof + ",p=" + clientProof.toString("base64")
})
this.connection.write(Buffer.concat([new Buffer(message.toString()), NULL_BUFFER]))
}
Variant Error
He has also reported a variant of the same error as posted below:
node ./bin/www
firefund:www Listening on port 3000 +0ms
node:events:498
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:371:5)
at _write (node:internal/streams/writable:319:11)
at Socket.Writable.write (node:internal/streams/writable:334:10)
at /home/edel_weiss/firefund-production/node_modules/rethinkdbdash/lib/connection.js:143:23
at Object.tryCatch (/home/edel_weiss/firefund-production/node_modules/rethinkdbdash/lib/helper.js:170:3)
at Socket.<anonymous> (/home/edel_weiss/firefund-production/node_modules/rethinkdbdash/lib/connection.js:142:12)
at Socket.emit (node:events:523:35)
at Socket.emit (node:domain:475:12)
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1143:10)
Emitted 'error' event on Connection instance at:
at Socket.<anonymous> (/home/edel_weiss/firefund-production/node_modules/rethinkdbdash/lib/connection.js:129:12)
at Socket.emit (node:events:520:28)
at Socket.emit (node:domain:475:12)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
This variant suggest that the error is at line 143 in connection.js:
self.connection.write(Buffer.concat([versionBuffer, authBuffer, NULL_BUFFER]));
self.connection.on('connect', function() {
self.connection.removeAllListeners('error');
self.connection.on('error', function(error) {
self.emit('error', error);
});
var versionBuffer = new Buffer(4)
versionBuffer.writeUInt32LE(protodef.VersionDummy.Version.V1_0, 0)
self.randomString = new Buffer(crypto.randomBytes(18)).toString('base64')
var authBuffer = new Buffer(JSON.stringify({
protocol_version: PROTOCOL_VERSION,
authentication_method: AUTHENTIFICATION_METHOD,
authentication: "n,,n=" + self.user + ",r=" + self.randomString
}));
helper.tryCatch(function() {
self.connection.write(Buffer.concat([versionBuffer, authBuffer, NULL_BUFFER]));
}, function(err) {
// The TCP connection is open, but the ReQL connection wasn't established.
// We can just abort the whole thing
self.open = false;
reject(new Err.ReqlDriverError('Failed to perform handshake with '+self.host+':'+self.port).setOperational());
});
});
If you read this far - THANK YOU!
Both errors are about writing to a Buffer.
Is anyone aware of any related issues to nodejs's Buffer implementation on Windows 11?
Definitely not sure if this is THE answer, just spit-balling here that maybe you could make a custom Buffer.concat function to work in case it really is the nodejs Buffer.concat being the cause of the problem here ;-; hope it works I'm on windows 10 however
function BufferConcat(buffers){ //buffers is the array of buffers
var bytes=Buffer.byteLength, i=0
var length=buffers.reduce((b1,b2)=>bytes(b1)+bytes(b2))
const BufferToReturn=Buffer.alloc(length)
for(let buffer in buffers){
let values=Object.values(buffer)
for(let item of buffer){BufferToReturn[i++]=item}
}
return BufferToReturn
}

Using mariadb nodeJS connector with multiple queries

I've got a server running Node.js connecting to a MariaDb database. I'm using the Node.js connector and it's working fine for single queries. However, when I try to do multiple queries it's throwing this error:
{ Error: (conn=8439, no: 1064, SQLState: 42000) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 2' at line 1
sql: SELECT 1; SELECT 2; - parameters:[]
at Object.module.exports.createError (\node_modules\mariadb\lib\misc\errors.js:55:10)
at Packet.readError (\node_modules\mariadb\lib\io\packet.js:506:19)
at Query.readResponsePacket (\node_modules\mariadb\lib\cmd\resultset.js:47:28)
at PacketInputStream.receivePacket (\node_modules\mariadb\lib\io\packet-input-stream.js:73:9)
at PacketInputStream.onData (\node_modules\mariadb\lib\io\packet-input-stream.js:129:20)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
fatal: false,
errno: 1064,
sqlState: '42000',
code: 'ER_PARSE_ERROR' }
The current test code I'm using is:
conn = await pool.getConnection({multipleStatements: true});
conn.query({
multipleStatements: true,
sql: "SELECT 1; SELECT 2;"
}).then((data: any) => {
console.log(data);
conn.end();
});
The documentation suggests multipleStatements is an option, but looking through the GitHub Repo, the only places I've found it is under lib/config/connection-options.js and the GitHub documentation. I also noticed the batch option but that seems to be specifically for a load of inserts.
Is there a way to use the mariadb-connector-nodejs to run multiple queries in a single query call? If so, what else could I be doing wrong that only multi-statement queries are not working?
Versions:
- MariaDb: 10.1.34-MariaDB
- Node.js: v10.14.2
- mariadb-connector-nodejs: mariadb#2.0.2-rc
multipleStatements is the good option, but as #rolandstarke indicate, that is a connection option.
When using creating pool, you indicate pool + connection options, since pool will handle connection creation. see related documentation
Example:
const mariadb = require("mariadb");
const pool = mariadb.createPool({ multipleStatements: true });
pool.query("select 1; select 2")
.then(results => {
//select 1 results
console.log(results[0]); //{ '1': 1 }
//select 2 results
console.log(results[1]); //{ '2': 2 }
})
.catch(err => {
//handle error
});

NodeJS : Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:111:27)

Using Polling like below to check if the content of the file is changed then, other two functions are called
var poll_max_date=AsyncPolling(function (end,err) { if(err) {
console.error(err); } var stmp_node_id=fs.readFileSync(path.join(__dirname,'node_id'),"utf8");
console.log("--------loaded node : "+stmp_node_id);
if(druid_stmp_node_id!=stmp_node_id) {
// MAX DATA CUT-OFF DRUID QUERY
druid_exe.max_date_query_fire();
// // DRUID QUERY FOR GLOBAL DATA
druid_exe.global_druid_query_fire();
druid_stmp_node_id=stmp_node_id; }
end(); }, 1800000).run();//30 mins
Its working fine for sometime, but then getting below error like after 4 - 5hours :
events.js:167
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:111:27) Emitted 'error' event at:
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
tried using fs.watch to monitor the changes in the file instead of polling like below :
let md5Previous = null; let fsWait = false;
fs.watch(dataSourceLogFile, (event, filename) => { if (filename) {
if (fsWait) return;
fsWait = setTimeout(() => {
fsWait = false;
}, 1000);
const md5Current = md5(fs.readFileSync(dataSourceLogFile));
if (md5Current === md5Previous) {
return;
}
md5Previous = md5Current;
console.log(`${filename} file Changed`);
// MAX DATA CUT-OFF DRUID QUERY
druid_exe.max_date_query_fire();
// DRUID QUERY FOR GLOBAL DATA
druid_exe.global_druid_query_fire(); } });
Its is also working fine for sometime, but then getting same error like after 4 - 5hours :
events.js:167 throw er; // Unhandled 'error' event ^
Error: read ECONNRESET at TCP.onStreamRead
(internal/stream_base_commons.js:111:27) Emitted 'error' event at: at
emitErrorNT (internal/streams/destroy.js:82:8) at emitErrorAndCloseNT
(internal/streams/destroy.js:50:3)
But when run in Local Machine, its working fine. the error occurs only when run in remote Linux Machine.
somebody can help me how I can fix that problem?
Use fs.watchFile once , because fs.watch is not consistent across platforms,
https://nodejs.org/docs/latest/api/fs.html#fs_fs_watchfile_filename_options_listener
Change your code according to the requirement.
It has been happening since the users are closing the browser before the data request is received, leading to Connection Reset.
Used PM2 (http://pm2.keymetrics.io/) to run the application, and it is working great now .

Redis -nodejs simple program -ERROR

I am getting error for even a simple nodejs redis commands.
This is the error I am getting.
/home/veera/Radha/node_modules/redis-client/lib/redis-client.js:394
var callback = originalCommand[originalCommand.length - 1];
^
TypeError: Cannot read property 'length' of undefined
at Client.onReply_ (/home/veera/Radha/node_modules/redis-client/lib/redis-client.js:394:51)
at maybeCallbackWithReply (/home/veera/Radha/node_modules/redis-client/lib/redis-client.js:143:30)
at ReplyParser.feed (/home/veera/Radha/node_modules/redis-client/lib/redis-client.js:183:29)
at Socket. (/home/veera/Radha/node_modules/redis-client/lib/redis-client.js:337:28)
at Socket.emit (events.js:95:17)
at Socket. (_stream_readable.js:765:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:427:10)
at emitReadable (_stream_readable.js:423:5)
at readableAddChunk (_stream_readable.js:166:9)
And the code is,
var client = require("./redis-node-client/lib/redis-client").createClient();
client.set('test', 'data');
My redis version is ,
redis-server --version
Redis server v=3.0.4 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=4e722bd58502cba0
https://github.com/fictorial/redis-node-client/issues/26 - same issue has been discussed long time back but the version they are discussing is 1.2.
My version is 3.0.4.
Please help me to fix this issue.
It looks like this library is no more supported. ( Description itself says abandoned project.) Unless you have strict requirement, use actively developed and production ready library like node-redis.
Example using node-redis
var client = require("node-redis").createClient();
client.set('test', 'data');
client.get('test',function(err, data){
if(err) console.log(err);
else console.log('reply from redis:' + data);
});
client.quit();

rs/pushd crashes when a new subscriber is received

I'm trying to use https://github.com/rs/pushd as a push notification server.
Whenever I try to add a new subscriber, the server crashes with this stack trace:
/home/ec2-user/push_server/pushd/node_modules/redis/index.js:602
throw err;
^
TypeError: Cannot set property 'id' of null
at /home/ec2-user/push_server/pushd/lib/api.coffee:20:21
at /home/ec2-user/push_server/pushd/lib/subscriber.coffee:133:21
at try_callback (/home/ec2-user/push_server/pushd/node_modules/redis/index.js:592:9)
at RedisClient.return_reply (/home/ec2-user/push_server/pushd/node_modules/redis/index.js:685:13)
at HiredisReplyParser.<anonymous> (/home/ec2-user/push_server/pushd/node_modules/redis/index.js:321:14)
at HiredisReplyParser.emit (events.js:95:17)
at HiredisReplyParser.execute (/home/ec2-user/push_server/pushd/node_modules/redis/lib/parser/hiredis.js:43:18)
at RedisClient.on_data (/home/ec2-user/push_server/pushd/node_modules/redis/index.js:547:27)
at Socket.<anonymous> (/home/ec2-user/push_server/pushd/node_modules/redis/index.js:102:14)
at Socket.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:765:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:427:10)
at emitReadable (_stream_readable.js:423:5)
at readableAddChunk (_stream_readable.js:166:9)
at Socket.Readable.push (_stream_readable.js:128:10)
at TCP.onread (net.js:529:21)
This is the command that should add a new subscriber (example from the documentation), and causes the crash:
curl -d proto=apns -d token=FE66489F304DC75B8D6E8200DFF8A456E8DAEACEC428B427E9518741C92C6660 -d lang=fr -d badge=0 -d category=show -d contentAvailable=true http://localhost/subscribers
The crash happens before the subscriber is added to the database.
The server is a RHEL micro instance on AWS
Versions
Redis Server 2.8.19.
Node.js 0.10.36.
CoffeeScript 1.9.0
Any help will be appreciated.
/lib/event.coffee
- throw new Error("Missing redis connection") if not redis?
+ throw new Error("Missing redis connection") if not #redis?
/lib/subscriber.coffee
- if info?.updated? # subscriber exists
+ if #info?.updated? # subscriber exists
# transform numeric value to number type
- for own key, value of info
+ for own key, value of #info

Resources