I am creating a RESTful API using Node.js , express and My sql. I have created 2 Services,2Routers, and 2 Controllers
App.js module
require("dotenv").config();
const express = require("express");
const app = express();
const userRouter = require("./api/Routers/user.router");
const categoryRouter=require("./api/Routers/category.router");
app.use(express.json());
app.use("/api/users", userRouter);
app.use("/api/category", categoryRouter);
const port = process.env.PORT ;
app.listen(port, () => {
console.log("server up and running on PORT :", port);
});
Category.service.js
const pool = require("../../config/database");
module.exports = {
create: (data, callBack) => {
pool.query(
`insert into category(name, , branch, state, create_date, modified_date)
values(?,?,?,?,?,?)`,
[
data.name,
data.branch,
data.state,
data.create_date,
data.modified_date,
],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results);
}
);
},
getUserByUserEmail: (email, callBack) => {
pool.query(
`select * from registration where email = ?`,
[email],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results[0]);
}
);
},
getCategoryByCategoryId: (id, callBack) => {
pool.query(
`select id,name,branch,state,create_date,modified_date from category where id = ?`,
[id],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results[0]);
}
);
},
getCategory: callBack => {
pool.query(
`select id,name,branch,state,create_date,modified_date from category`,
[],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results);
}
);
},
updateCategory: (data, callBack) => {
pool.query(
`update category set name=?, branch=?, state=?, create_date=?, modified_date=?, `,
[
data.name,
data.branch,
data.state,
data.create_date,
data.modified_date,
],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results[0]);
}
);
},
deleteCategory: (data, callBack) => {
pool.query(
`delete from category where id = ?`,
[data.id],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results[0]);
}
);
}
};
Category.route.js
const router = require("express").Router();
const { checkToken } = require("../../auth/token_validation");
const {
createCategory,
login,
getCreateByCreateId,
getCategory,
updateCategory,
deleteCategory
} = require("../Controllers/category.controller");
router.get("/", checkToken, getCategory);
router.post("/", checkToken, createCategory);
router.get("/:id", checkToken, getCreateByCreateId);
router.post("/login", login);
router.patch("/", checkToken, updateCategory);
router.delete("/", checkToken, deleteCategory);
module.exports = router;
Category.controll.js
const {
create,
getUserByUserEmail,
getCategoryByCategoryId,
getCategory,
updateCategory,
deleteCategory
} = require("../services/category.service");
const { hashSync, genSaltSync, compareSync } = require("bcrypt");
const { sign } = require("jsonwebtoken");
module.exports = {
createUser: (req, res) => {
const body = req.body;
const salt = genSaltSync(10);
body.password = hashSync(body.password, salt);
create(body, (err, results) => {
if (err) {
console.log(err);
return res.status(500).json({
success: 0,
message: "Database connection error"
});
}
return res.status(200).json({
success: 1,
data: results
});
});
},
login: (req, res) => {
const body = req.body;
getUserByUserEmail(body.email, (err, results) => {
if (err) {
console.log(err);
}
if (!results) {
return res.json({
success: 0,
data: "Invalid email or password"
});
}
const result = compareSync(body.password, results.password);
if (result) {
results.password = undefined;
const jsontoken = sign({ result: results }, process.env.JWT_KEY, {
expiresIn: "1h"
});
return res.json({
success: 1,
message: "login successfully",
token: jsontoken
});
} else {
return res.json({
success: 0,
data: "Invalid email or password"
});
}
});
},
getCategoryByCategoryId: (req, res) => {
const id = req.params.id;
getCategoryByCategoryId(id, (err, results) => {
if (err) {
console.log(err);
return;
}
if (!results) {
return res.json({
success: 0,
message: "Record not Found"
});
}
results.password = undefined;
return res.json({
success: 1,
data: results
});
});
},
getCategory: (req, res) => {
getCategory((err, results) => {
if (err) {
console.log(err);
return;
}
return res.json({
success: 1,
data: results
});
});
},
updateCategory: (req, res) => {
const body = req.body;
const salt = genSaltSync(10);
body.password = hashSync(body.password, salt);
updateCategory(body, (err, results) => {
if (err) {
console.log(err);
return;
}
return res.json({
success: 1,
message: "updated successfully"
});
});
},
deleteCategory: (req, res) => {
const data = req.body;
deleteCategory(data, (err, results) => {
console.log(results);
if (err) {
return;
}
if (!results) {
return res.json({
success: 0,
message: "Record Not Found",
h:results
});
}
return res.json({
success: 1,
message: "category deleted not successful"
});
});
}
};
I am getting Error: Route.post() requires a callback function but got a [object Undefined. how can i sort it out
Error: Route.post() requires a callback function but got a [object Undefined] how can I sort this error
createUser should be renamed in createCategory in Category.controll.js
I am New To async/await coding in node js. I am trying to fetch data from mysql and then populate the result into an object. But i am not finding a way how to do it in controller.js file.
My source code similar to this.
My Router File
const express = require("express");
const controller = require("controller");
const router = express.Router();
router.route("/").get(controller.findAll);
My Controller File
const model = require("model");
exports.findAll = async (req, res) => {
const all = model.getAll((err, data) => {
if (err) {
res.status(500).send({
message: err.message || "Some error occurred while retrieving data.",
});
}
return data;
});
const user = model1.getUser((err, data) => {
if (err) {
res.status(500).send({
message: err.message || "Some error occurred while retrieving user.",
});
}
return data;
});
// somefunctionality(all, user);
// res.send(result);
};
My Model file
const con = require("./db");
// constructor
const Customers= function (customer) {
this.id = customer.id;
this.first_name = customer.first_name;
this.last_name = customer.last_name;
this.email = customer.email;
};
Customers.getAll = () => {
const query = `SELECT * FROM doctors`;
sql.query(query, (err, res) => {
if (err) {
console.log("error: ", err);
result(null, err);
return;
}
console.log("customers: ", res);
result(null, res);
};
user model same as customer
Thanks for your help
The first thing I would do is covert your Customers.getAll method to return a promise:
model file
Customers.getAll = () => {
const query = `SELECT * FROM doctors`;
return new Promise((res, rej) => {
sql.query(query, (err, data) => {
if (err) {
console.log("error: ", err);
rej(err);
return;
}
console.log("customers: ", res);
res(data);
})
};
(Note that you'll probably have to do something similar to the getAll method for your User model)
Now, in the controller, you can use try/catch to handle any errors.
*controller file
exports.findAll = async (req, res) => {
try {
const all = await model.getAll();
const users = await model1.getUser();
const result = somefunctionality(all, user);
res.send(result);
} catch(err) {
res.status(500).send({
message: err.message || "Some error occurred while retrieving data.",
});
}
};
My basic POST API makes a call to database , first to authenticate user and then to authentic if the products provided are true. But before productValidation can complete, res.end() is being executed.
As output, I get ::
"validated object"
"stopping the res"
"validated object"
Whereas I am trying to validated and add each cartItem to database and only then send a success response to client.
const express = require("express");
const app = express();
const router = express.Router();
var connection = require("./../database/serverConnector");
router.get("/:userId", (req, res, next) => {
connection.query(
"SELECT * FROM cart_items AS c INNER JOIN product AS p ON c.product_id = p.id WHERE user_id = ?",
[req.params.userId],
function (error, results, fields) {
if (error) throw error;
else {
console.log("Query made for cart of user " + req.params.userId);
res.status(200).send(results);
}
}
);
});
router.post("/:userId", userValidate, (req, res, next) => {
multipleProductValidation(req, res, next).then(function (data) {
res.end();
console.log("stopping the res");
});
// .catch(error){
// console.log(error);
// }
});
function userValidate(req, res, next) {
connection.query(
"SELECT * from user where id = ?",
[req.params.userId],
function (error, user, fields) {
if (error) {
res.status(500).json({
message: error.sqlMessage,
});
} else {
if (user.length == 0) {
res.status(404).json({
message: "User not found with ID " + req.params.userId,
});
} else next();
}
}
);
}
function multipleProductValidation(req, res, next) {
return new Promise(function (resolve, reject) {
req.body.cartItems.forEach((item, i) => {
productValidate(item, req, res, next).then(function (data) {
resolve(data);
console.log("validated object");
//next();
});
});
});
}
function productValidate(item, req, res, next) {
return new Promise(function (resolve, reject) {
connection.query(
"SELECT * from product where id = ?",
[item.product_id],
function (error, product, fields) {
if (error) {
res.status(500).json({
message: error.sqlMessage,
});
} else {
if (product.length == 0) {
res.status(404).json({
message: "Product not found with ID " + item.product_id,
});
resolve(false);
} else {
connection.query(
"INSERT INTO cart_items (product_id,user_id,size,quantity) VALUES (?,?,?,?)",
[item.product_id, req.params.userId, item.size, item.quantity],
function (error, results, fields) {
if (error) {
res.status(500).json({
message: error.sqlMessage,
});
} else {
res.statusText =
"Items added to cart for User " + req.params.userId;
resolve(true);
}
}
);
}
}
}
);
});
}
module.exports = router;
So what is happening here is that you are calling resolve in your multipleProductValidation before you finish processing all the products.
something like this should fix your problem
function multipleProductValidation(req, res, next){
return new Promise(function(resolve, reject) {
let promiseArr = req.body.cartItems.map(item => productValidate(item, req, res, next))
Promise.all(promiseArr).then(values => resolve(values))
}
)}
I have my main routes file called main.js, in which I want to call sql functions from a module called database.js Within the database.js file I am able to retrieve the data, however when passing to main.js it keep saying that data is undefined.
main.js
const database = require('./modules/database')
app.get('/', async(req, res) => {
try {
const data = database.getAll()
console.log(data)
res.render('home', {stock: data})
} catch(err) {
console.log(err.message)
res.render('empty')
}
})
database.js
const sqlite3 = require('sqlite3').verbose()
const db = new sqlite3.Database('./stockInventory.db', (err) => {
if (err) return console.error(err.message)
console.log('Connected to the "stockInventory.db" SQlite database.')
})
module.exports.getAll = () => {
const sql = 'SELECT * FROM stock;'
console.log(sql)
db.all(sql, (err, data) => {
if(err) console.error(err.message)
//console.log(data)
return data
})
}
You need callback function, Try this
const database = require('./modules/database')
app.get('/', async(req, res) => {
try {
database.getAll(function(data) {
console.log(data)
res.render('home', {stock: data})
})
} catch(err) {
console.log(err.message)
res.render('empty')
}
})
module.exports.getAll = (callback) => {
const sql = 'SELECT * FROM stock;'
console.log(sql)
db.all(sql, (err, data) => {
if(err) console.error(err.message)
//console.log(data)
callback(data);
})
}
I want to call a stored procedure / function in postgresql from node/express. I am using Angular 5.
Installed packages:
express: 4.16.2,
pg: ^7.4.0
Other required packages are also installed
Below is the code of my API - I want to replace all queries with my stored procedure / function call
const express = require('express');
const router=express.Router();
const {Pool,Client} = require('pg');
const connectionString = 'postgresql://postgres:1#localhost:5432/dbAngular'
// const client = new Client({
// connectionString:connectionString
// })
router.get('/employees', (err, res, done) => {
const client = new Pool({
connectionString:connectionString
})
client.connect((err)=>{
if(err!=undefined)
{
console.log('connection not established message as follow '+err.message+'')
}
});
client.query('select * from getEmployees();', (err, result) => {
if (err) {
console.log(err.stack) ;
client.end();
} else {
client.end();
console.log(result.rows);
res.json(result.rows);
}
})
})
//SAVE
router.post('/employees', (req, res,next) => {
const client = new Pool({
connectionString:connectionString
})
console.log( req.body);
var employee=req.body;
const query = {
text: 'INSERT INTO employes(name) VALUES($1)',
values: [employee.name]
}
client.connect((err)=>{
if(err!=undefined)
{
console.log('connection not established '+err.message+'')
}
});
client.query(query, (err, result) => {
if (err) {
console.log('ERRROOORRRRR');
client.end();
console.log(err.stack)
} else {
console.log('SAVEEEEE');
client.end();
// console.log(res.rows[0])
res.json();
}
})
});
//delete
router.delete('/employees/:id', (req, res,next) => {
const client = new Pool({
connectionString:connectionString
});
console.log('id passed is ' + req.param.id);
client.query('DELETE FROM employes WHERE empid=($1)',[req.param.id], (err, result) => {
if (err) {
console.log('ERRROOORRRRR');
client.end();
console.log(err.stack)
} else {
console.log('SAVEEEEE');
client.end();
// console.log(res.rows[0])
res.json();
}
});
});
module.exports=router;