Node.js SyntaxError: Unexpected token - javascript

I have defined the routers for the api as follows, one for the post and get,
var bookRouter = express.Router();
bookRouter.route('/Books')
.post(function(req, res) {
var bok = new book(req.body);
console.log(bok);
res.send(bok);
})
.get(function(req, res) {
var query = req.query;
book.find(query, function(err, books) {
if (err)
res.status(500).send(err);
else
res.json(books);
});
});
it throws an error saying '.get(function(req, res) {' Unexpected token .
Node version :
v4.3.0
Parser used :
body-parser

var express = require("express");
var router = express.Ruoter();
router.post("/Books",function(req,res){
var bok = new book(req.body);
console.log(bok);
res.send(bok);
});
router.get("/Book/?id",function(req.res){
var query = req.params.id;
book.find(query, function(err, books) {
if (err)
res.status(500).send(err);
else
res.json(books);
})

Related

Node express mysql second route call crashes

I am trying to set an API. when the default route is called, with '/', the route can be called several times but the route '/count' can only be called once before crashing with this error:
/Users/node_modules/mysql/lib/protocol/Parser.js:437
throw err; // Rethrow non-MySQL errors
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:467:11)
at ServerResponse.header (/Users/node_modules/express/lib/response.js:771:10)
at ServerResponse.send (/Users/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/Users/node_modules/express/lib/response.js:267:15)
at ServerResponse.send (/Users/node_modules/express/lib/response.js:158:21)
at Query.<anonymous> (/Users/server_sql/testingAPI/routes/index.js:25:36)
at Query.<anonymous> (/Users/node_modules/mysql/lib/Connection.js:525:10)
at Query._callback (/Users/node_modules/mysql/lib/Connection.js:491:16)
at Query.Sequence.end (/Users/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
at Query._handleFinalResultPacket (/Users/node_modules/mysql/lib/protocol/sequences/Query.js:139:8)
app.js
const http = require('http');
const cors = require('cors');
const body_parser = require('body-parser');
const databases = require('./connection_management');
const route = require('./routes/index');
const app = express();
app.use(cors());
app.use(body_parser.urlencoded({
extended: true
}));
app.use(body_parser.json());
app.use(databases.connectionsSql);
app.use('/', route);
http.createServer(app).listen(8080, (err) => {
if (!err)
console.log("API ready and HTTP listen on port 8080.");
else
console.log(err)
});
module.exports = app;
route/index.js
var router = require('express').Router();
var connection = require('../connection_management');
router.get('/', function(req, res){
res.send('Default route');
});
function getCo(){
return connection.coSql['test'].pool;
}
router.get('/count', function(req, res){
var pool = getCo();
var countQuery = "SELECT COUNT(data) FROM table WHERE data IS NOT NULL";
try {
pool.getConnection(function(errco, connection){
if(errco) return res.send(errco);
connection.query(countQuery, function(error, result){
if (error) throw error;
else {
try{
connection.release();
return res.send(result);
} catch (err) {
return res.send(err);
}
}
});
})
}catch (err){
return res.send(err);
}
})
module.exports = router;
connection_management.js
const fs = require('fs');
var mysql = require('mysql');
var config = fs.readFileSync('config.json');
config = JSON.parse(config);
const coSql = [];
exports.connectionsSql = (req, res, next) => {
for (let i = 0; i < config.sql.databases.length; i++) {
if (coSql[config.sql.databases[i].name]) {
next();
} else {
coSql[config.sql.databases[i].name] = {};
coSql[config.sql.databases[i].name].pool = mysql.createPool({
host: config.sql.databases[i].host,
user: config.sql.user,
password: config.sql.password,
database: config.sql.databases[i].name});
}
}
next();
}
module.exports.coSql = coSql;
config.js
{
"sql":{
"user": "foo",
"password": "bar",
"databases":[
{
"name": "test",
"host": "localhost"
}
]
}
}
Thanks for your help, fixing and understanding the problem.
In route/index.js:
router.get('/count', function(req, res){
var pool = getCo();
var countQuery = "SELECT COUNT(data) FROM table WHERE data IS NOT NULL";
try {
pool.getConnection(function(errco, connection){
if(errco) throw New Error(errco); // <---- CHANGED THIS.
connection.query(countQuery, function(error, result){
if (error) throw error;
else {
try{
connection.release();
return res.send(result);
} catch (err) {
throw New Error(err); // <---- CHANGED THIS.
}
}
});
})
}catch (err){
return res.send(err);
}
})
You might be calling res.send more than once in your code due to the way you've written you error catching.
I have replaced 2 of your res.send() method calls with thrown errors so that they are caught and dealt with in the final catch block at the bottom.

pendingItem.callback is not a function?

I am trying to view the first row of a table but I get the error
TypeError: pendingItem.callback is not a function
at client.connect
var express = require("express");
const {Pool} = require("pg");
var app = express();
var conStr = "postgres://postgres:password#localhost:5432/postgres";
const pool = new Pool();
app.get("/", function(req, res, next) {
pool.connect(conStr, function(err, client, done) {
if (err) {
console.log("not able to get connection " + err);
res.status(400).send(err);
}
client.query("SELECT * FROM Users where id= $1", [1], function(err, result) {
done();
if (err) {
console.log(err);
res.status(400).send(err);
}
res.status(200).send(result.rows);
});
});
});
On Postgres connection Pool using connectionString
First if you are choosing to connect to Postgres using connectionURI, you need to initialize Pool with connectionString param:
const pool = new Pool({
connectionString: connectionString,
})
Then when calling pool.connect you only need to pass callback function:
pool.connect((err, client, release) => {...});
Check documentation on Pooling and different ways of making a connection to Postgres using node-postgres package: here and here
In your case this, it should look like this:
var express = require("express");
const pg = require("pg");
const {Pool} = require("pg");
var app = express();
var conStr = "postgres://postgres:password#localhost:5432/postgres";
const pool = new Pool({
connectionString: conStr
});
app.get("/", function(req, res, next) {
pool.connect(function(err, client, done) {
if (err) {
console.log("not able to get connection " + err);
res.status(400).send(err);
}
client.query("SELECT * FROM Users where id= $1", [1], function(err, result) {
done();
if (err) {
console.log(err);
res.status(400).send(err);
}
res.status(200).send(result.rows);
});
});
});

trying to get the array result for mongodb in expressjs

I'm trying to get a result from MongoDB database return as a response for my GET Request when I make the call I'm getting undefined in the response, I'm able to see the result in console log on the database side of the code. I'm feeling like it might have something to do with it being an array
MongoClient = require('mongodb'),
mongodburl = "mongodb://localhost:27017",
mongo = require('mongodb'),
assert = require('assert');
let method={}
method.insertdata=function(data,collectionpara) {
MongoClient.connect(mongodburl, function (err, db) {
assert.equal(null, err);
let dbo = db.db("hor-usData");
let myobj = data;
dbo.collection(collectionpara).insert(myobj, function (err, result) {
assert.equal(null, err);
console.log("insert data");
db.close();
})
})
}
method.deleteRecords=function(collectionpara) {
MongoClient.connect(mongodburl, function (err, db) {
assert.equal(null, err);
let dbo = db.db("hor-usData");
var myquery = {
title: /^B/
};
dbo.collection(collectionpara).deleteMany(myquery, function (err, obj) {
if (err) throw err;
console.log(obj.result.n + " document(s) deleted");
db.close();
})
})
}
method.getdata=function(collectionpara){
MongoClient.connect(mongodburl, function(err, db) {
if (err) throw err;
let dbo = db.db("hor-usData");
dbo.collection(collectionpara).find().toArray(function(err, result) {
if (err){
return reject(err)
}
console.log('result',result);
return resolve(result);
});
});
}
module.exports = method;
and my router code
let assert = require('assert'),
express = require('express'),
router = express(),
swaggerUi = require('swagger-ui-express'),
database=require('../databaseCon'),
// swaggerDocument = require('./swagger.json'),
utils = require('../utils/utils');
var port = process.env.PORT || 3000;
//get donedeal
router.get('/getDonedeal', function (req, res, next) {
let donedealResult = []
donedealResult=database.getdata('donedeal');
res.send(donedealResult);
});
//get carzone
router.get('/getCarzone', function (req, res, next) {
let carzoneResult = [];
carzoneResult=database.getdata('carzone');
res.send(console.log(carzoneResult));
});
router.get('/cars', function (req, res, next) {
res.router('Mainrouter')
});
router.get('/getCarzone', function (req, res, next) {
var resultArray = []
});
router.listen(port);
//app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
//app.use('/api/v1', router);
module.exports = router;

Getting null value while sending the data into mssql using node js

It would be great if anyone can help me out. I am new to node.js, and I am trying to send the data into MSSQL database for my initial project, and as per project requirement I was not able to use any other DB other than MSSQL, and I am getting error value into DB while I execute the insert query. I tried to figure out the error more than a day but end up with nothing. Could anyone help me to fix this error.?
Thanks in advance.
// Server.js
var express = require('express');
var app = express();
var port = process.env.port || 3000;
var bodyParser = require('body-parser');
// create application/x-www-form-urlencoded parser
app.use(bodyParser.urlencoded({ extended: true }));
// create application/json parser
app.use(bodyParser.json());
var ProductController = require('./Controller/ProductController')();
app.use("/app/Data", ProductController)
app.listen(port, function () {
var datetime = new Date();
var message = "Server runnning on Port:- " + port + "Started at :- " + datetime;
console.log(message);
});
// App.js
var express = require('express');
var router = express.Router();
var sql = require("mssql");
var conn = require("../connection/connect")();
var routes = function () {
router.route('/')
.get(function (req, res) {
conn.connect().then(function () {
var sqlQuery = "SELECT * FROM ArduinoSensor";
var req = new sql.Request(conn);
req.query(sqlQuery).then(function (recordset) {
res.json(recordset.recordset);
conn.close();
})
.catch(function (err) {
conn.close();
res.status(400).send("Error while Receive Data");
});
})
.catch(function (err) {
conn.close();
res.status(400).send("Error");
});
});
router.route('/')
.post(function (req, res) {
conn.connect().then(function () {
var transaction = new sql.Transaction(conn);
transaction.begin().then(function () {
var request = new sql.Request(transaction);
request.input("start_time", sql.VarChar(50), req.body.start_time)
request.input("end_time", sql.VarChar(50), req.body.end_time)
request.input("length_time", sql.VarChar(50), req.body.length_time)
request.execute("Usp_InsertSensor").then(function () {
transaction.commit().then(function (recordSet) {
conn.close();
res.status(200).send(req.body);
}).catch(function (err) {
conn.close();
res.status(400).send("Error while inserting data");
});
}).catch(function (err) {
conn.close();
res.status(400).send("Error while inserting data");
});
}).catch(function (err) {
conn.close();
res.status(400).send("Error while inserting data");
});
}).catch(function (err) {
conn.close();
res.status(400).send("Error while inserting data");
});
});
return router;
};
module.exports = routes;
PS:- I have attached the outcome of the coding
Outcome of the image

Best practise to define a PostgreSQL database connection variable globally in a node.js web application?

I am creating a node and express REST application with a PostgreSQL database.
My question is how to define the connection variable globally in a minimalist express application (for a Hello World example)?
I have the following file structure with the following key files included.
{PROJECT_ROOT}\bin\www
{PROJECT_ROOT}\app.js
{PROJECT_ROOT}\routes\index.js
{PROJECT_ROOT}\db\db.js
{PROJECT_ROOT}\db\location.js
{PROJECT_ROOT}\OTHER FILES
The db.js should contain definition of a variable for a connection to the PostgreSQL database globally. This variable should be shared by other modules whenever necessay so that duplicated connections should be avoided.
db.js
var promise = require('bluebird');
/**
*Use dotenv to read .env vars into Node
*/
require('dotenv').config();
const options = {
// Initialization Options
promiseLib: promise,
connect(client, dc, useCount) {
const cp = client.connectionParameters;
console.log('Connected to database:', cp.database);
}
};
const pgp = require('pg-promise')(options);
const connectionString = process.env.PG_CONN_STR;
const db = pgp(connectionString);
module.exports = {
pgp, db
};
location.js defines the business logic to manipulate the gcur_point_location table.
var db_global = require('./db');
var db = db_global.db;
// add query functions
module.exports = {
getAllLocations: getAllLocations,
getLocation: getLocation,
createLocation: createLocation,
updateLocation: updateLocation,
removeLocation: removeLocation
};
function getAllLocations(req, res, next) {
db.any('select * from gcur_point_location')
.then(function (data) {
res.status(200)
.json({
status: 'success',
data: data,
message: 'Retrieved ALL GCUR Point Locations'
});
})
.catch(function (err) {
return next(err);
});
}
function getLocation(req, res, next) {
var locationId = parseInt(req.params.id);
db.one('select * from gcur_point_location where locationid = $1', locationId)
.then(function (data) {
res.status(200)
.json({
status: 'success',
data: data,
message: 'Retrieved ONE Location by Id'
});
})
.catch(function (err) {
return next(err);
});
}
function createLocation(req, res, next) {
req.body.age = parseInt(req.body.age);
db.none('insert into gcur_point_location(locationname, locationstatus, lng, lat)' +
'values(${locationname}, ${locationstatus}, ${lng}, ${lat})',
req.body)
.then(function () {
res.status(200)
.json({
status: 'success',
message: 'Inserted one Location'
});
})
.catch(function (err) {
return next(err);
});
}
function updateLocation(req, res, next) {
db.none('update gcur_point_location set locationname=$1, locationstatus=$2, lng=$3, lat=$4 where locationid=$5',
[req.body.locationname, req.body.locationstatus, parseFloat(req.body.lng),
parseFloat(req.body.lat), parseInt(req.params.id)])
.then(function () {
res.status(200)
.json({
status: 'success',
message: 'Updated Location'
});
})
.catch(function (err) {
return next(err);
});
}
function removeLocation(req, res, next) {
var locationId = parseInt(req.params.id);
db.result('delete from gcur_point_location where locationid=$1', locationId)
.then(function (result) {
/* jshint ignore:start */
res.status(200)
.json({
status: 'success',
message: `Removed ${result.rowCount} Location`
});
/* jshint ignore:end */
})
.catch(function (err) {
return next(err);
});
}
Likewise, munipulation of different tables will be defined in individual js files. All of them will require the db.js.
routes/index.js
var express = require('express');
var router = express.Router();
var db = require('../db/location');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/api/locations', db.getAllLocations);
router.get('/api/location/:id', db.getLocation);
router.post('/api/location', db.createLocation);
router.put('/api/location/:id', db.updateLocation);
router.delete('/api/location/:id', db.removeLocation);
module.exports = router;
app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
I would like to have some ideas about whether the above code is a good or a bad practise or any potential failure?
Most of the code makes sense to me, though I would implement your own ORM and model layers, so you can remove some of the code for PSQL queries and follow MVC design pattern. If all you are building is an express api server, then you do not need the View portion.
I usually have a file called ORM, which has something similar to the following:
var orm = {
all: function(tableInput, cb) {
var queryString = "SELECT * FROM " + tableInput + ";";
connection.query(queryString, function(err, result) {
if (err) {
throw err;
}
cb(result);
});
},
create: function(table, cols, vals, cb) {
var queryString = "INSERT INTO " + table;
queryString += " (";
queryString += cols.toString();
queryString += ") ";
queryString += "VALUES (";
queryString += printQuestionMarks(vals.length);
queryString += ") ";
console.log(queryString);
connection.query(queryString, vals, function(err, result) {
if (err) {
throw err;
}
cb(result);
});
},
// An example of objColVals would be {name: panther, sleepy: true}
update: function(table, objColVals, condition, cb) {
var queryString = "UPDATE " + table;
queryString += " SET ";
queryString += objToSql(objColVals);
queryString += " WHERE ";
queryString += condition;
console.log(queryString);
connection.query(queryString, function(err, result) {
if (err) {
throw err;
}
cb(result);
});
}
};
// Export the orm object for the model (cat.js).
module.exports = orm;
Then I define a model file for each table you have in psql as following:
// Import the ORM to create functions that will interact with the database.
var orm = require("../config/orm.js");
var cat = {
all: function(cb) {
orm.all("cats", function(res) {
cb(res);
});
},
// The variables cols and vals are arrays.
create: function(cols, vals, cb) {
orm.create("cats", cols, vals, function(res) {
cb(res);
});
},
update: function(objColVals, condition, cb) {
orm.update("cats", objColVals, condition, function(res) {
cb(res);
});
}
};
// Export the database functions for the controller (catsController.js).
module.exports = cat;
A controller:
var express = require("express");
var router = express.Router();
// Import the model (cat.js) to use its database functions.
var cat = require("../models/cat.js");
// Create all our routes and set up logic within those routes where required.
router.get("/", function(req, res) {
cat.all(function(data) {
var hbsObject = {
cats: data
};
console.log(hbsObject);
res.render("index", hbsObject);
});
});
router.post("/api/cats", function(req, res) {
cat.create(["name", "sleepy"], [req.body.name, req.body.sleepy], function(result) {
// Send back the ID of the new quote
res.json({ id: result.insertId });
});
});
router.put("/api/cats/:id", function(req, res) {
var condition = "id = " + req.params.id;
console.log("condition", condition);
cat.update(
{
sleepy: req.body.sleepy
},
condition,
function(result) {
if (result.changedRows === 0) {
// If no rows were changed, then the ID must not exist, so 404
return res.status(404).end();
}
res.status(200).end();
}
);
});
// Export routes for server.js to use.
module.exports = router;
This follows MVC design pattern which is very easy to read and understand. So my whole folder structure would look something like this:
Best practices for structuring a database layer with pg-promise are shown in pg-promise-demo.
For a complete, real-world example of using that approach, see LISK database layer.

Categories

Resources