Call sql functions in node.js modules using module.exports - node.js

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);
})
}

Related

Why my function for retrieving data from mongodb is returning undefined?

I am trying to return all the entries from a mongodb collection from a nodejs.
I had written the function and it works if i console log the result i see all the objects from the colletion, but if i try to return the result i am getting undefined.
I cant figure it out why? I had also tried to JSON stringify and JSON parse after but still no success.
Here is my code:
`
const mongoUrl = "mongodb://192.168.8.156:27017/";
const getRoomReadings = function (id) {
MongoClient.connect(mongoUrl, function (err, db) {
if (err) throw err;
let dbo = db.db(`room${id}`);
dbo
.collection("env")
.find({})
.toArray(function (err, result) {
if (err) throw err;
return result;
});
});
};
// API RoomX route
app.get("/api/r:id", (req, res) => {
const rez = getRoomReadings(req.params.id);
console.log(rez);
});
`
I am using nodejs with express.
Please help me. Thanks in advance.
I had also tried to JSON stringify and JSON parse after but still no success.
I don't know why you created the connection each time you do the request but using promises will help you.
Example:
const mongoUrl = "mongodb://192.168.8.156:27017/";
const getRoomReadings = function (id) {
return new Promise((res, rej) => {
MongoClient.connect(mongoUrl, function (err, db) {
if (err) rej(err);
let dbo = db.db(`room${id}`);
dbo
.collection("env")
.find({})
.toArray(function (err, result) {
if (err) rej(err);
return res(result);
});
});
})
};
// API RoomX route
app.get("/api/r:id", async (req, res) => {
const rez = await getRoomReadings(req.params.id);
console.log(rez);
});
a better way to create a connection it creating a file call conn.js and inside that file create your connection
const { MongoClient } = require("mongodb");
const connectionString = process.env.ATLAS_URI;
const client = new MongoClient(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
let dbConnection;
module.exports = {
connectToServer: function (callback) {
client.connect(function (err, db) {
if (err || !db) {
return callback(err);
}
dbConnection = db.db(<db_name>);
console.log("Successfully connected to MongoDB.");
return callback();
});
},
getDb: function () {
return dbConnection;
},
};
initialize the connection and use getDb to get the connection

Convert my existing Node Js code into Async Await

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.",
});
}
};

How to insert large array object in mongoose mongodb collection with the help of node worker thread?

I want to insert a large array object in a Mongoose collection with the help of node worker thread, but it is not responding. I tried it without node worker thread and it's working fine, but with worker thread it's not working.
This is my worker file worker.js:
const { isMainThread, parentPort } = require("worker_threads");
const User = require("./models/user");
const csv = require("csvtojson");
if (!isMainThread) {
console.log("In Worker Thread");
parentPort.on("message", (csvFilePath) => {
csv()
.fromFile(csvFilePath)
.then((jsonObj) => {
User.insertMany(jsonObj)
.then((res) => {
console.log(res);
parentPort.postMessage("result");
})
.catch((err) => {
parentPort.postMessage("error");
console.log(err);
});
});
});
}
This is my main tread file. From here I am sending the data CSV file path. With that path, I will read the CSV file and convert it to the array of JSON. I want to insert this array into a MongoDB collection with the help of Mongoose.
// main thread file
const app = require("express")();
const formidable = require("formidable");
const mongoose = require("mongoose");
const User = require("./models/user");
const csv = require("csvtojson");
mongoose.connect("mongodb://localhost:27017/fileUpload", {
useNewUrlParser: "true",
useUnifiedTopology: true,
});
mongoose.connection.on("error", (err) => {
console.log("err", err);
});
mongoose.connection.on("connected", (err, res) => {
console.log("mongoose is connected");
});
const { isMainThread, Worker } = require("worker_threads");
let worker;
if (isMainThread) {
console.log("In main thread");
worker = new Worker("./worker.js");
worker.on("message", (data) => {
// console.log("Got Message=>", data);
});
worker.on("error", (data) => {});
worker.on("exit", (data) => {});
}
app.get("/", (req, res) => {
worker.postMessage("from main thread");
res.status(200).json("Hellow World");
});
app.post("/upload", (req, res, next) => {
const form = formidable({ multiples: true });
form.parse(req, (err, fields, files) => {
if (err) {
next(err);
return;
}
const csvFilePath = files.file.path;
csv()
.fromFile(csvFilePath)
.then((jsonObj) => {
console.log(jsonObj);
worker.postMessage(csvFilePath);
worker.on("message", (data) => {
res.json({ fields, files, data });
});
});
});
});
app.listen(5001, () => {
console.log("Server Is Running");
});

Express: how to end response

I new in Nodejs and faced some problems and need help
I broke down my app into modules and controllers
in app.js
app.use('/api/sync', syncDataRouter)
and in syncDataRouter.js
const routes = function (con, Reading, ReadingSummary) {
const syncDataRouter = express.Router();
const syncDataController = require('../controller/syncDataController')(con, Reading, ReadingSummary);
//Get All Readings from Database and push it to Mongo-db
syncDataRouter.route('/')
.get(syncDataController.sync);
return syncDataRouter;
};
module.exports = routes;
and in controller
const syncDataController = function (con, Reading, ReadingSummary) {
function getAllCompletedSessions() {
return new Promise((resolve, reject) => {
const sql = 'SELECT * FROM session WHERE endTime IS NOT NULL';
con.query(sql, (err, rows) => {
if (err) reject(new Error(err));
resolve(rows);
});
});
};
function getSessionPlayers(sessionId) {
return new Promise((resolve, reject) => {
const sql = 'SELECT * FROM sessionplayers WHERE sessionId = ?';
con.query(sql, [sessionId], (err, rows) => {
if (err) reject(new Error(err));
resolve(rows);
});
});
};
const sync = (req, res) => {
getAllCompletedSessions()
.then(sessions => {
})
.catch(err => {
res.status(500).send({ message: err.message });
});
//The Problem
res.json('Done');
};
return {
sync: sync
}
}
module.exports = syncDataController;
the problem is if any error happened enter a catch block and also continue until res.json('Done'); and sent a message can't set headers after they are sent how to handle a situation like that
Try making the sql queries async
const syncDataController = function (con, Reading, ReadingSummary) {
async function getAllCompletedSessions() {
return new Promise((resolve, reject) => {
const sql = 'SELECT * FROM session WHERE endTime IS NOT NULL';
await con.query(sql, (err, rows) => {
if (err) reject(new Error(err));
resolve(rows);
});
});
};
async function getSessionPlayers(sessionId) {
return new Promise((resolve, reject) => {
const sql = 'SELECT * FROM sessionplayers WHERE sessionId = ?';
await con.query(sql, [sessionId], (err, rows) => {
if (err) reject(new Error(err));
resolve(rows);
});
});
};
const sync = (req, res) => {
getAllCompletedSessions()
.then(sessions => {
})
.catch(err => {
res.status(500).send({ message: err.message });
});
//The Problem
res.json('Done');
};
return {
sync: sync
}
}
module.exports = syncDataController;

Node/express postgresql stored procedure / function

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;

Resources