Heroku: Javascript/MySQL project crashing on request - javascript

I have a Heroku API written in JavaScript that reads data from a MySQL database. It previously worked, but now every time I send a request to the API, I get this error:
2020-06-17T18:37:13.493711+00:00 app[web.1]: > my_api#0.0.0 start /app
2020-06-17T18:37:13.493712+00:00 app[web.1]: > node ./bin/www
2020-06-17T18:37:13.493712+00:00 app[web.1]:
2020-06-17T18:37:14.977800+00:00 heroku[web.1]: State changed from starting to up
2020-06-17T18:38:40.255982+00:00 app[web.1]: /app/node_modules/mysql2/lib/connection.js:149
2020-06-17T18:38:40.256022+00:00 app[web.1]: err.fatal = true;
2020-06-17T18:38:40.256023+00:00 app[web.1]: ^
2020-06-17T18:38:40.256023+00:00 app[web.1]:
2020-06-17T18:38:40.256026+00:00 app[web.1]: TypeError: Cannot create property 'fatal' on string 'SOCKS: Authentication failed'
2020-06-17T18:38:40.256027+00:00 app[web.1]: at Connection._handleFatalError (/app/node_modules/mysql2/lib/connection.js:149:15)
2020-06-17T18:38:40.256027+00:00 app[web.1]: at Connection._handleNetworkError (/app/node_modules/mysql2/lib/connection.js:169:10)
2020-06-17T18:38:40.256027+00:00 app[web.1]: at SocksConnection.emit (events.js:315:20)
2020-06-17T18:38:40.256029+00:00 app[web.1]: at /app/node_modules/socksjs/socks.js:146:18
2020-06-17T18:38:40.256029+00:00 app[web.1]: at Socket.dataReady (/app/node_modules/socksjs/socks.js:93:13)
2020-06-17T18:38:40.256029+00:00 app[web.1]: at Socket.emit (events.js:315:20)
2020-06-17T18:38:40.256030+00:00 app[web.1]: at emitReadable_ (_stream_readable.js:556:12)
2020-06-17T18:38:40.256030+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:83:21)
2020-06-17T18:38:40.263200+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-06-17T18:38:40.263391+00:00 app[web.1]: npm ERR! errno 1
2020-06-17T18:38:40.264251+00:00 app[web.1]: npm ERR! my_api#0.0.0 start: `node ./bin/www`
2020-06-17T18:38:40.264354+00:00 app[web.1]: npm ERR! Exit status 1
2020-06-17T18:38:40.264471+00:00 app[web.1]: npm ERR!
The code in question looks like this:
function new_db_connection() {
var sockConn = new SocksConnection(remote_options, sock_options);
var dbConnection = mysql.createConnection({
user: 'myusername',
database: 'mydatabase',
password: 'mypassword',
stream: sockConn
});
return [sockConn, dbConnection];
};
router.get('/myrequest', function(req, res, next) {
new_conn = new_db_connection();
dbConnection = new_conn[1];
sockConn = new_conn[0];
dbConnection.query('SELECT * from mytable', function(error, results, fields) {
if (error) {
res.send(JSON.stringify({
"status": 500,
"error": error,
"response": null
}));
//If there is error, we send the error in the error section with 500 status
} else {
res.send(JSON.stringify({
"status": 200,
"error": null,
"response": results
}));
//If there is no error, all is good and response is 200OK.
}
sockConn.dispose();
});
dbConnection.end()
});
I saw a similar question asked on here and the solution was to close the connection, but I do close the connection after each request, so I don't know where the issue is.
All help appreciated.

Try to change this
sockConn.dispose();
});
dbConnection.end()
});
in this
sockConn.dispose();
dbConnection.end()
});
});
Edit: reading better the error, it could be the problem is some where else.
Looking at the string 'SOCKS: Authentication failed', could it be that you or the SocksConnection remote endpoint changed something in the authentication?
Hope this helps.

You should take a look at the answer provided by #daniele Rici you need to end your connection properly; however, fixing this will not solve your initial question.
The issue is not with your code. Your issue is with connection configuration.
'fatal' on string 'SOCKS: Authentication failed'
Means there was an error with authentication.
var sockConn = new SocksConnection(remote_options, sock_options);
under the assumention you are using ther npm package socksjs from https://openbase.io/js/socksjs
It is documented that the correct way to initialize a sockjs connection is by doing the following:
var sock = new SocksConnection(remote_options, sock_options);
var sock = SocksConnection.connect(remote_options, sock_options, connect_handler);

Related

Discord.js missing access permision what should do?

I've my own bot but last 2 days I got this error I don't know why? Maybe some command but my bot only can send message and embed message that's why not more permission i don't think so it needed.
Or how can I show which permission is needed?
/home/runner/TruthOrDarejs/node_modules/discord.js/src/rest/RequestHandler.js:349
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Missing Permissions
at RequestHandler.execute (/home/runner/TruthOrDarejs/node_modules/discord.js/src/rest/RequestHandler.js:349:13)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (/home/runner/TruthOrDarejs/node_modules/discord.js/src/rest/RequestHandler.js:50:14)
at async MessageManager.react (/home/runner/TruthOrDarejs/node_modules/discord.js/src/managers/MessageManager.js:193:5)
at async Message.react (/home/runner/TruthOrDarejs/node_modules/discord.js/src/structures/Message.js:720:5) {
method: 'put',
path: '/channels/855192585206366221/messages/919613273811333160/reactions/%E2%9C%85/#me',
 npm start
> TruthOrDarejs#1.0.0 start /home/runner/TruthOrDarejs
> node .
Server is ready.
DB connection sucessfull
Truth Or Dare#7260 Has logged in
/home/runner/TruthOrDarejs/node_modules/discord.js/src/rest/RequestHandler.js:349
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Missing Permissions
at RequestHandler.execute (/home/runner/TruthOrDarejs/node_modules/discord.js/src/rest/RequestHandler.js:349:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (/home/runner/TruthOrDarejs/node_modules/discord.js/src/rest/RequestHandler.js:50:14)
at async GuildInviteManager.create (/home/runner/TruthOrDarejs/node_modules/discord.js/src/managers/GuildInviteManager.js:185:20)
at async Client.<anonymous> (/home/runner/TruthOrDarejs/index.js:298:20) {
method: 'post',
path: '/channels/913210496147132479/invites',
code: 50013,
httpStatus: 403,
requestData: {
json: {
temporary: false,
max_age: 0,
max_uses: 0,
unique: undefined,
target_user_id: null,
target_application_id: undefined,
target_type: undefined
},
files: []
}
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! TruthOrDarejs#1.0.0 start: `node .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the TruthOrDarejs#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/runner/.npm/_logs/2021-12-12T16_48_03_527Z-debug.log
I added some new permission in the dev portal. but still it gets the error :(

Why is this POST method not inserting data in my Postgres database in Node?

EDIT: This is the error I recieved:
C:\Users\przyb\Desktop\lister\puupy\server.js:33
if (err) throw err;
^
error: missing FROM-clause entry for table "body"
at Parser.parseErrorMessage (C:\Users\przyb\Desktop\lister\puupy\node_modules\pg-protocol\dist\parser.js:241:15)
at Parser.handlePacket (C:\Users\przyb\Desktop\lister\puupy\node_modules\pg-protocol\dist\parser.js:89:29)
at Parser.parse (C:\Users\przyb\Desktop\lister\puupy\node_modules\pg-protocol\dist\parser.js:41:38)
at Socket.stream.on (C:\Users\przyb\Desktop\lister\puupy\node_modules\pg-protocol\dist\index.js:8:42)
at Socket.emit (events.js:189:13)
at addChunk (_stream_readable.js:284:12)
at readableAddChunk (_stream_readable.js:265:11)
at Socket.Readable.push (_stream_readable.js:220:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! puupy#1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the puupy#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\przyb\AppData\Roaming\npm-cache\_logs\2020-07-19T01_30_59_800Z-debug.log
I am trying to insert data into a table in my postgres database using an HTML form and Node for server side. Here is the form:
<form action="/" method="POST">
<input id="Name" type="text" max="100" value="Doggy" name="name">
<input id="query" type="submit" value="Submit">
Code to connect to db:
const Pool = require("pg").Pool;
const pool = new Pool({
user:"postgres",
host:"localhost",
database:"puppy",
password:"Bolek_3003",
port:5432
});
And this is how I am trying to actually insert the data:
var bodyParser = require("body-parser");
var urlencodedParser = bodyParser.urlencoded({extended: false});
app.post('/', urlencodedParser, function(req,res,next){
pool.query("INSERT INTO Users VALUES(null, req.body.name)",(err,res)=>{
if (err) throw err;
console.log(err,res)
});
pool.end()
});
I don't know for how this data to actually be accepted into the database, thanks in advance.
I think there is a mistake in how the request is formatted
const query = {
text: 'INSERT INTO Users(column1, column2) VALUES($1, $2)',
values: [null, `${req.body,name}`],
}
pool.query(query,(err,res)=>{...//})
or this
const query = 'INSERT INTO Users(column1, column2) VALUES($1, $2)',
values= [null, `${req.body,name}`]
}
pool.query(query,values,(err,res)=>{...//})

authorization failed emailjs heroku

Using emailjs to send emails on my nodejs application.
However, it shows the error below when deployed on heroku, it's strange because application works locally
2018-01-28T22:28:24.689274+00:00 app[web.1]: { Error:
authorization.failed (timedout while connecting to smtp server)
2018-01-28T22:28:24.689286+00:00 app[web.1]: at module.exports
(/app/node_modules/emailjs/smtp/error.js:2:13)
2018-01-28T22:28:24.689290+00:00 app[web.1]: at failed
(/app/node_modules/emailjs/smtp/smtp.js:501:28)
2018-01-28T22:28:24.689292+00:00 app[web.1]: at response
(/app/node_modules/emailjs/smtp/smtp.js:506:13)
2018-01-28T22:28:24.689293+00:00 app[web.1]: at caller
(/app/node_modules/emailjs/smtp/smtp.js:48:14)
2018-01-28T22:28:24.689295+00:00 app[web.1]: at response
(/app/node_modules/emailjs/smtp/smtp.js:198:9)
2018-01-28T22:28:24.689296+00:00 app[web.1]: at caller
(/app/node_modules/emailjs/smtp/smtp.js:48:14)
2018-01-28T22:28:24.689298+00:00 app[web.1]: at TLSSocket.response
(/app/node_modules/emailjs/smtp/smtp.js:178:11)
2018-01-28T22:28:24.689300+00:00 app[web.1]: at Object.onceWrapper
(events.js:315:30)
2018-01-28T22:28:24.689302+00:00 app[web.1]: at emitOne
(events.js:116:13)
2018-01-28T22:28:24.689304+00:00 app[web.1]: at TLSSocket.emit
(events.js:211:7)
2018-01-28T22:28:24.689307+00:00 app[web.1]: code: 3,
2018-01-28T22:28:24.689308+00:00 app[web.1]: previous: { Error:
timedout while connecting to smtp server
2018-01-28T22:28:24.689310+00:00 app[web.1]: at module.exports
(/app/node_modules/emailjs/smtp/error.js:2:13)
2018-01-28T22:28:24.689312+00:00 app[web.1]: at TLSSocket.timedout
(/app/node_modules/emailjs/smtp/response.js:32:29)
2018-01-28T22:28:24.689313+00:00 app[web.1]: at Object.onceWrapper
(events.js:313:30)
2018-01-28T22:28:24.689314+00:00 app[web.1]: at emitNone
(events.js:106:13)
2018-01-28T22:28:24.689315+00:00 app[web.1]: at TLSSocket.emit
(events.js:208:7)
2018-01-28T22:28:24.689316+00:00 app[web.1]: at
TLSSocket.Socket._onTimeout (net.js:407:8)
2018-01-28T22:28:24.689318+00:00 app[web.1]: at ontimeout
(timers.js:475:11)
2018-01-28T22:28:24.689319+00:00 app[web.1]: at tryOnTimeout
(timers.js:310:5)
2018-01-28T22:28:24.689320+00:00 app[web.1]: at Timer.listOnTimeout
(timers.js:270:5) code: 4, smtp: undefined },
2018-01-28T22:28:24.689322+00:00 app[web.1]: smtp: undefined }
My code for stmp authentication
var server = email.server.connect({
user: email,
password: pass,
host: "smtp-mail.outlook.com",
tls: { ciphers: "SSLv3" }
});
I already tried
var https = require('https');
https.globalAgent.options.secureProtocol = 'SSLv3_method';
There's existing post for the similar problem, however, in this scenario i'm using outlook as opposed to other servcies

Node.js - events.js throw er

Before posting this question I went through a couple of SO posts with the same heading. All of them suggested me to restart my app..I tried that but I am still getting the error.
The code that is throwing the error is
app.post('/insert', function(request, response) {
var connection = mySequel.createConnection(ConnectionParams);
connection.connect();
var UserName = request.body.UserName;
// insert into userrecords values ("123456",DEFAULT,DEFAULT,DEFAULT,10);
var InsertQuery = "insert into userrecords values (" + UserName + ",DEFAULT,DEFAULT,DEFAULT,-10);";
connection.query(InsertQuery, function(error, result, fields) {
if (error) {
response.send("error");
console.log(error);
throw error;
} else {
// response.send("success");
console.log(result.insertId);
response.send(result.insertId);
}
});
connection.end();
});
The error caused by the code is this
events.js:160
2017-04-26T11:42:52.410094+00:00 app[web.1]:
2017-04-26T11:42:52.410092+00:00 app[web.1]: throw er; // Unhandled 'error' event
2017-04-26T11:42:52.410096+00:00 app[web.1]: Error: Quit inactivity timeout
2017-04-26T11:42:52.410097+00:00 app[web.1]: at Quit.<anonymous> (/app/node_modules/mysql/lib/protocol/Protocol.js:160:17)
2017-04-26T11:42:52.410093+00:00 app[web.1]: ^
2017-04-26T11:42:52.410098+00:00 app[web.1]: at emitNone (events.js:86:13)
2017-04-26T11:42:52.410099+00:00 app[web.1]: at Quit._onTimeout (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:127:8)
2017-04-26T11:42:52.410099+00:00 app[web.1]: at Quit.emit (events.js:185:7)
2017-04-26T11:42:52.410100+00:00 app[web.1]: at ontimeout (timers.js:380:14)
2017-04-26T11:42:52.410101+00:00 app[web.1]: at tryOnTimeout (timers.js:244:5)
2017-04-26T11:42:52.410102+00:00 app[web.1]: at Timer.listOnTimeout (timers.js:214:5)
I am hosting my app on Heroku and whenever I try to access this URL the app is crashing forcing me to restart the dyno. The data I am sending is being successfully inserted into the DB but the auto incremented primary key is not being returned to my app. Instead I get a server error.
JSON.stringify() returns the correct value once and after that the app crashed which maked the subsequent requests to fail.
How can I solve this problem?
Try converting your response into JSON before sending it.
Like
response.send(JSON.stringify(result.insertId));
I too once had a similar problem and I think that is how I solved it.
EDIT:
I went through the myql npm library and found something called Pool. Pool is used when you app needs to handle more than one request at a time. I suggest that you try that. There is a good example availble here.
I have same problem, i just downgrade to react-scripts#2.1.8
this is the steps:
cd my-app
npm install react-scripts#2.1.8
npm start

nodejs and heroku: throw new Error('"name" and "value" are required for setHeader().');

Can you please help, I get the Application Error on heroku, although locally app works fine.
Seems this is the problem in _http_outgoing.js:333:
throw new Error('"name" and "value" are required for setHeader().');
More specifically:
2015-04-07T05:22:23.244579+00:00 app[web.1]: > node ./bin/www
2015-04-07T05:22:23.845835+00:00 app[web.1]: throw new Error('"name" and "value" are required for setHeader().');
2015-04-07T05:22:23.845840+00:00 app[web.1]: at ClientRequest.OutgoingMessage.setHeader (_http_outgoing.js:333:11)
2015-04-07T05:22:23.845845+00:00 app[web.1]: at /app/node_modules/simplecrawler/lib/crawler.js:768:11
2015-04-07T05:22:23.845847+00:00 app[web.1]: at process._tickCallback (node.js:355:11)
2015-04-07T05:22:23.845848+00:00 app[web.1]: at Function.Module.runMain (module.js:503:11)
2015-04-07T05:22:23.845837+00:00 app[web.1]: ^
2015-04-07T05:22:23.845838+00:00 app[web.1]: Error: "name" and "value" are required for setHeader().
And my app.js
var app = express();
var port = process.env.PORT || 5000
var server = http.createServer(app)
server.listen(port)
console.log("http server listening on %d", port)
How can I fix this?
From what the error is saying, and based off your title, you appear to be just running the setHeader function without arguments, when this is a value setter function, and requires two arguments: The first (name) is the name of the header whose value you would like to set, and the second (value) is the value you would like to set the header to. Hope this clears things up!
The problem was with the simplecrawler module. Implemented a new version, where this problem is fixed, and it worked.

Categories

Resources