PostgreSQL : password authentication failed for user "sean" - javascript

So I've looked around all over the place for an answer for this but I cannot find one anywhere.
so I'm trying to setup a PostgreSQL database with nodeJS and I've installed pgadmin3 etc.
here is my code
const express = require("express");
const pg = require("pg");
const connect = "postgres://sean:sean#localhost/karls"
var pool = new pg.Pool(connect);
router.route("/").get((req, res) => {
pool.connect(function(err, client, done){
if(err) return console.error("the error ocurred on: " + err);
client.query("SELECT * FROM Recipe", function(err, result) {
if(err) console.error(error);
console.log(results)
done();
})
})
});
on my pgadmin3 I have a login role named sean as a super user so I don't know why it's not connecting I have the password correct but it keeps spitting out this error:
error: password authentication failed for user "sean";
I've been trying to fix this for ages if you need any more information let me know and ill update the question
I've solved this problem :)

Related

How to solve an error while sending a get request in chatbot using ibm watson?

when I send a get request on postman session id is not generated instead it goes on catch block and shows an error occured, please help me solve this problem
const authenticator = new IamAuthenticator({
apikey: process.env.WA_ASSISTANT_APIKEY,
});
const assistant = new AssistantV2({
version: "2019-02-28",
authenticator: authenticator,
url: process.env.WA_ASSISTANT_APIKEY,
});
router.get("/session", async (req, res) => {
//if successfull
try{
const session = await assistant.createSession({
assistantId: process.env.WA_ASSISTANT_ID,
});
res.json(session["result"]);
//error
}catch(err){
res.send("An Error occured while processiong your request!");
console.log(err);
}
})
This is the code
Error
The error suggests that your app is trying to connect to Watson Assistant on localhost. I suspect the error is due to your line -
url: process.env.WA_ASSISTANT_APIKEY
APIKey is very unlikely to be a url.

Failed to validate receiptError: failed to validate purchase using Node.js

I am trying to set up a node.js server to validate receipts from AppStore connect In App Purchases I have set up. I have followed this https://github.com/voltrue2/in-app-purchase library but I'm getting an error response saying my receipt is not defined and failed to validate receipt. I'm I doing anything wrong here. I'm still testing on a local server. I want to get it to work before hosting it on Heroku. What would I be doing wrong here?
const iap = require('in-app-purchase');
iap.config({
applePassword: 'MySecretKey',
test: true
});
iap.setup()
.then(() => {
iap.validateOnce(receipt, appleSecretString).then(onSuccess).catch(onError);
})
.catch((error) => {
if (error) {
console.log('Validation error' + error);
}
});
iap.validate(iap.APPLE, function (error, appleResponse) {
console.log(iap.APPLE);
if (error) {
console.log('Failed to validate receipt' + error);
}
if (iap.isValidated(appleResponse)) {
console.log('Validation successful');
}
});
Here are the logs
iapserver:server Listening on port 3000 +0ms
Validation errorReferenceError: receipt is not defined
apple
Failed to validate receiptError: failed to validate purchase
From the error is seems like you're not actually passing the receipt file into the validateOnce function. Somewhere before that code runs you need to set:
const receipt = req.query.receipt;
And invoke your API with something like:
http://localhost:3000/verifyReceipt?receipt=<YOUR_RECEIPT_DATA>
You can verify your receipt beforehand manually to make sure it's valid.
Putting this all together, you'd get something like:
var express = require('express');
var app = express();
const iap = require('in-app-purchase');
app.get('/verifyReceipt', function(req, res){
const receipt = req.query.receipt;
iap.config({
applePassword: 'MySecretKey',
test: true
});
iap.setup()
.then(() => {
iap.validateOnce(receipt, appleSecretString).then(onSuccess).catch(onError);
})
.catch((error) => {
if (error) {
console.log('Validation error' + error);
res.status(400).send({valid: false});
}
});
iap.validate(iap.APPLE, function (error, appleResponse) {
console.log(iap.APPLE);
if (error) {
console.log('Failed to validate receipt' + error);
res.status(400).send({valid: false});
}
if (iap.isValidated(appleResponse)) {
console.log('Validation successful');
res.status(200).send({valid: true});
}
});
});
app.listen(3000);
Note that this will only verify the receipt at purchase, if you're implementing subscriptions you also need to periodically refresh the receipt to make sure they user didn't cancel. Here's a good guide on implementing subscriptions: iOS Subscriptions are Hard

Failing to connect to MongoDB hosted on mlab

Background
Making a small web app that connects to a Mongo DB hosted with Mlab. I've created the DB on mlab, and created users with read/write permission. I've also created a users collection with several records.
The Problem
When I try and connect to the database using the code on mongo.github.io, I hit the error:
/home/ed/dev/mongo-demo/node_modules/mongodb/lib/operations/mongo_client_ops.js:466
throw err;
^
TypeError: Cannot read property 'db' of null
The Code
var MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://<user>:<pass>#ds115434.mlab.com:15434';
// Database Name
const dbName = 'princee3-music';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");
const db = client.db(dbName);
client.close();
});
What I Have Tried
Oddly, if I connect through the shell using:
mongo ds115434.mlab.com:15434/princee3-music -u <dbuser> -p <dbpassword>
That works fine, or if I wrap the connection in an anonymous self-calling async function, it also connects.
Async Wrapper
const MongoClient = require('mongodb').MongoClient;
const mongoUrl = 'mongodb://<user>:<pass>#ds115434.mlab.com:15434/';
const dbName = 'princee3-music';
(async() => {
const client = await MongoClient.connect(mongoUrl, { useNewUrlParser: true});
const db = client.db(dbName);
db.collection('users').insertOne({
email: user.email,
pass: hashedPassword,
admin: true
}, (err, result) => {
if (err) {
reject({error: err});
} else {
resolve({message: 'okay'});
}
});
client.close();
})();
Any pointers on where I may be going wrong would be great.
The official mLab docs advise to connect like below. It has to be asynchronous , in order to wait for the connection to occur, or the client will be null, thus throwing an error saying that it can’t read property db of null.
On the other hand, you async has useNewUrlParser which might be the key to have a successful connection, see this issue
MongoClient.connect(url, { useNewUrlParser: true }).then(client => client.db())

Node-postgres pool.connect becomes unresponsive

Hiii,
Recently my elastic beanstalk server have started to become unresponsive and return's 504 gateway timeout. I suspect that my pool.connect is becoming unresponsive and there are no logs reporting error while connecting to pool but it stucks at connecting. My other controllers with no database query works fine.
This goes away when I restart the server but after some time same thing happens.
I making requests this way-
1) database.js
const pg = require("pg")
// setting timestamp for the postgres.
pg.types.setTypeParser(1184, function(stringValue)
{
console.log(stringValue)
return new Date(Date.parse(Date.parse(stringValue + "+0000")))
})
// configuration for postres for connecting.
const pgConfig = {
user: "USER",
database: "DATABASE",
password: "1234",
port: 5432
}
const pool = new pg.Pool(pgConfig)
module.exports = pool
2) somecontroller.js
const db = require("../database/database")
const constant = require("../utility/constant")
module.exports = function(req, res)
{
db.connect(function(err, client, done)
{
if(err)
{
done()
console.log(constant.error.db.CONNECT_CONSOLE, err)
return res.send({status: constant.status.ERROR, code: constant.error.db.CONNECT})
}
client.query("QUERY", [UID])
.then(result =>
{
// Processing this queries and Some other query.....
})
.catch(err =>
{
console.log(constant.error.db.QUERY_CONSOLE, err)
res.send({status: constant.status.ERROR, code: constant.error.db.QUERY})
})
done()
})
}
My every controller works in similar fashion.
Thanks,

How to use node-mysql correctly with Express.js?

I'm wondering how to use the module node-mysql correctly in Node.js (using Express.js). I have a main router with this:
var Post = require('./models/post.js');
app.get('/archives', function (req, res) {
Post.findArchives(function(posts, err) {
if(err)
res.send('404 Not found', 404);
else
res.render('archives', { posts: posts});
});
});
And here's the content of the file post.js:
var mysql = require('mysql');
var dbURL = 'mysql://root#localhost/mydatabase';
exports.findArchives = function(callback) {
var connection = mysql.createConnection(dbURL);
connection.query('SELECT * FROM blog_posts_view WHERE status != 0 ORDER BY date DESC', function(err, rows) {
if(err) throw err
callback(rows, err);
connection.end();
});
};
How can I improve it? Improve the error handling? Also, there's the function handleDisconnect(connection); on their Github (https://github.com/felixge/node-mysql) that I'm not really sure how to integrate to make sure that the application will not crash when the database is not responding.
Thanks!
Take a look at the mysql-simple library. It combines node-mysql with a pooling library to create a connection pool, and also includes the code to handle the disconnects.
If you want to make it super easy, you could just use that module.

Categories