node-postgresql db connection deprecated - postgresql

I am using node-v8.9.1-x64. & postgresql-10.1-2. I have tried running this code but its gives an error "code deprecated" and even tried some other ways. It still comes back with new errors
app.get('/', function(req, res) {
// PG connect -code deprecated
pg.connect(connect, function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT * FROM books', function(err, result) {
if(err) {
return console.error('error running query', err);
}
res.render('index', {books: result.rows});
done();
});
});
I went through the documentation but I am not getting it. Please somebody help me.

Related

On using MongoDb Client v3.0, it shows the error “db.collection” not a function

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/testdb";
MongoClient.connect(url, function(err, db) {
if (err)
throw err;
db.collection("OTPs").findOne({}, function(err, result) {
if (err)
throw err;
console.log(OTPs.name);
db.close();
});
});
Error Message:
TypeError: db.collection is not a function
From version 3 the syntax have changed to get a db object as the function only provides a client object , then try the following
var url = "mongodb://localhost:27017/testdb";
MongoClient.connect(url, (err, client) => {
// Client returned
var db = client.db('mytestingdb');
if (err)
throw err;
db.collection("OTPs").findOne({}, function(err, result) {
if (err)
throw err;
console.log(OTPs.name);
db.close();
});
});

Mongo db connection not establishing

Below is the code which i used to up the server. It is running in the localhost server 3000.
app.use('/', books);
app.listen(3000, function(){
console.log('Server running at port 3000: http://localhost:3000')
});
Below code throws mongodb error. Dont know what exactly the error. Below is my code
var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://127.0.0.1:27017';
// SHOW LIST OF BOOKS
router.get('/book', function(req, res, next) {
MongoClient.connect(url, function(error, db) {
if (error) throw error;
var dbo = db.db("BookStore");
dbo.collection("Books").find().toArray(function(err, result) {
if (err) throw err;
res.send(result);
console.log(result);
db.close();
});
});
});
// SHOW BOOK with ISBN
router.get('/book/:isbn', function(req, res, next) {
var isbn = parseInt(req.params.isbn);
MongoClient.connect(url, function(error, db) {
if (error) throw error;
var dbo = db.db("BookStore");
var query = { "isbn": isbn };
dbo.collection("Books").findOne(query, function(err, result) {
if (err) throw err;
res.send(result);
console.log(result);
db.close();
});
});
});
// Add BOOK
router.post('/book', function(req, res, next) {
MongoClient.connect(url, function(error, db) {
if (error) throw error;
var dbo = db.db("BookStore");
var myobj = {
"isbn":req.body.isbn,
"publisher":req.body.publisher,
"title":req.body.title,
"authors":req.body.authors,
"publishedDate":req.body.publishedDate,
"price":req.body.price
};
dbo.collection("Books").insertOne(myobj, function(err, result) {
if (err) throw err;
res.send('Book with '+req.body.isbn+' is successfully added');
db.close();
});
});
});
// UPDATE BOOK
router.put('/book/:isbn', function(req, res, next) {
var isbn = parseInt(req.params.isbn);
MongoClient.connect(url, function(error, db) {
if (error) throw error;
var dbo = db.db("BookStore");
var query = { "isbn": isbn };
var newvalues = { $set: {
"publisher":req.body.publisher,
"title":req.body.title,
"authors":req.body.authors,
"publishedDate":req.body.publishedDate,
"price":req.body.price
}
};
dbo.collection("Books").updateOne(query, newvalues, function(err, result) {
if (err) throw err;
res.send('Book with '+req.params.isbn+' is successfully updated');
db.close();
});
});
});
// DELETE BOOK
router.delete('/book/:isbn', function(req, res, next) {
var isbn = parseInt(req.params.isbn);
MongoClient.connect(url, function(error, db) {
if (error) throw error;
var dbo = db.db("BookStore");
var query = { "isbn": isbn };
dbo.collection("Books").deleteMany(query, function(err, result) {
if (err) throw err;
res.send('Book with '+req.params.isbn+' is successfully removed');
db.close();
});
});
});
module.exports = router;
Code Explanation:
We are creating a route to our application called 'Books.' So
whenever anybody browses to http://localhost:3000/books of our application, the code snippet defined for this route will be
executed.
Here we are getting all of the records in our 'Books'
collection through the db.collection('Books').find() command. We are then assigning this collection to a variable called cursor. Using this cursor variable, we will be able to browse through all of the records of the collection.
Run sudo mongod in the terminal
Is it impossible to connect even if you change the value assigned to the variable url to mongodb://[user_name]:[password]#127.0.0.1:27017/[your_database_name]?authSource=admin ?
Actually, the error is related to MongoDB version(MongoClient.connect(url, function(error, db)), MongoDB developers have changed their API. I don't know which version, it throws an error when you use MongoClient.connect(url, function(error, db).
So it should be MongoClient.connect(url, function(err, client)
Link to api-doc
And do not forget to run mongod

MongoDB, Nodejs routing order

I have the problem with my routes. They work but only the one that's in the code first. The code below allows me to get a ticket by ID but not by registration number. If I put the second route above the first one it's vice versa.
What can I do to fix this?
//Get a ticket by ID
app.get('/tickets/:_id', function(req, res){
Ticket.getTicketById(req.params._id, function(err, ticket){
if(err){
throw err;
}
res.json(ticket);
});
});
//Get a ticket by registration number
app.get('/tickets/:vehRegistration', function(req, res){
Ticket.getTicketByReg(req.params.vehRegistration, function(err, ticket){
if(err){
throw err;
}
res.json(ticket);
});
});
You have written the same API path twice. :param_name can't be used to differentiate between two different paths. Try
//Get a ticket by ID
app.get('/tickets/byid/:_id', function(req, res) {
Ticket.getTicketById(req.params._id, function(err, ticket) {
if (err) {
throw err;
}
res.json(ticket);
});
});
//Get a ticket by registration number
app.get('/tickets/byreg/:vehRegistration', function(req, res) {
Ticket.getTicketByReg(req.params.vehRegistration, function(err, ticket) {
if (err) {
throw err;
}
res.json(ticket);
});
});

removing multiple document's references using mongoose

I'm currently trying to remove multiple documents references (objectId) when i'm removing those documents.
So this is my code to remove multiple documents and their references:
Comment.find({parentCommentId: req.params.id}).stream()
.on('data', function (comment) {
comment.remove(function (err, result) {
if (err){
console.log(err)
}
console.log(result);
})
})
.on('error', function(err){
// handle error
})
.on('end', function(){
res.status(200).json({
title: 'Comments deleted'
});
});
(this is the middleware i use)
schema.post('remove', function (comment) {
Project.findById(comment.project, function (err, project) {
if(err){
console.log('error : '+ err)
}
project.comments.pull(comment);
project.save();
});
User.findById(comment.user, function (err, user) {
if(err){
console.log('error : '+ err)
}
user.comments.pull(comment);
user.save();
});
});
At the moment i tried multiple things but i kinda got stuck: it removes all the documents i wanted but it always only removes the first document's reference and keeps the other ones.
at this point i don't really understand why.
Have a good day : )

testing facebook app with mocha

I'm developing a node.js app with Facebook login, I'm trying to do unit and integration tests with mocha. So in a function I have this code in order to login:
exports.loginFacebook = function (done){
request({uri:"https://graph.facebook.com//oauth/access_token?client_id={app-id}&client_secret={app-secret}&grant_type=client_credentials",
method: "GET",
timeout: reqOpts.timeout},
function(err, res, body) {
if(err){
throw err;
}
else{
uri2 = "https://graph.facebook.com/APP_ID/accounts/test-users?"+String(body);
request({uri:uri2,
method: "GET",
timeout: reqOpts.timeout},
function(err, res, body) {
if(err){
throw err;
}
else{
login_url = JSON.parse(body).data[0].login_url
console.log(login_url);
request({uri:login_url,
method: "GET",
timeout: reqOpts.timeout},
function(err, res, body) {
if(err){
throw err;
}
else{
request('http://localhost:4004/auth/facebook', reqOpts, function(err, res, body2) {
//console.log(res.statusCode)
//console.log(err)
done()
});
}
});
}
});
}
});
};
Basically I call Facebook API to get the access token, then I list my testing users and I finally I login with the first one. Problem: when I call the login_url, Facebook redirects to this webpage: http://i.imgur.com/V6NzuMi.jpg ("update your browser"), so I'm not able to do a correct login.
Any ideas how to solve this?
thanks!