Architecture in Node JS wirh Controller, Service and Repository - node.js

In order to keep clean architecture of my node.js I have controllers, services and repositories.
Data flow: controller -> service -> repository -> service -> controller.
In this flow (in simple user story) repository returns data to service and service to controller. But repository should process requests to external storage.
Right now I have the callback difficulty, how can I implement a nested callback between controller and repository?
My Controller:
exports.clientes_get = async function (req, res) {
Cliente.find(function(err,params) {
if (err)
res.send(err);
res.json(params);
});
}
My Service:
ClienteGet(req) {
var response;
repo.get(req.params.clienteId, (err, res) => {
response = res;
//console.log(response); -> have data
});
//console.log(response); -> doesnt have data
return response;
};
My Repository:
get(clienteId, data) {
mongoose.model('Cliente').findById(clienteId, data);
};
How can i do this?

Using Callbacks
If you insist on using callbacks:
Controller:
// notice that the `async` here was not necessary
exports.clientes_get = function (req, res) {
// the `function(err, params) {...}` here is your callback
ClienteGet(req, function(err, params) {
if (err) {
return res.send(err);
}
res.json(params);
});
}
Service:
ClienteGet(req, callback) {
// simply pass the callback argument directly to the `repo.get` call
repo.get(req.params.clienteId, callback);
};
Repository:
get(clienteId, callback) {
// again, just pass the callback here
mongoose.model('Cliente').findById(clienteId, callback);
};
Using Promises
I personally recommend wrapping you code in
Promises,
because they are more flexible than callbacks, and modern JavaScript uses
Promises heavily. It's pretty much the new standard... In fact, the async /
await syntax is leveraging Promises under the hood.
Here's a rough sketch of how you could use Promises:
Controller:
exports.clientes_get = function (req, res) {
return ClienteGet(req)
.then((data) => {
res.json(data);
})
.catch((err) => {
res.send(err);
});
}
Service:
ClienteGet(req) {
return new Promise((resolve, reject) => {
repo.get(req.params.clienteId, (err, res) => {
if (err) {
return reject(err);
}
resolve(res);
//console.log(response); -> have data
});
});
};
Repository:
get(clienteId) {
return new Promise((resolve, reject) => {
mongoose.model('Cliente').findById(clienteId, function(err, document) {
if (err) {
return reject(err);
}
resolve(document);
});
});
};

Related

Using SQL Server, nodejs and bycrypt, request ends before function is recieved

I'm fairly new to nodejs and have stumbled into a problem with my code.
The documentation for SQL Server and a guide I found on Youtube both handle their code this way, but after starting to use bycrypt I've noticed my function ends after the request is complete although I'm using .then().
Anyways, here's my code so far:
router.post('/login', (req, res) => {
getLoginDetails(req.body.username, req.body.password).then(result => {
console.log(result);
res.json(result);
})
});
async function getLoginDetails(username, password) {
await pool1Connect;
try {
const request = pool1.request();
request.input('username', sql.NVarChar, username);
request.query('SELECT * FROM users WHERE username = #username', (err, result) => {
if (err) {
return ({err: err})
}
if (result.recordset.length > 0) {
bcrypt.compare(password, result.recordset[0].user_password, (err, response) => {
if (response) {
console.log(result.recordset);
return(result.recordset);
} else {
return({message: "Wrong password or username!"})
}
})
return(result)
} else {
return({message: "user not found!"})
}
})
} catch (err) {
return err;
}
}
I tried logging both the request and the return value from the function getLoginDetails and the request log came faster, so I assume it's not waiting for the program to actually finish and I can't figure out why...
Sorry if that's obvious, but I'd love to get some help here!
EDIT:
router.post('/login', async (req, res) => {
// res.send(getLoginDetails(req.body.username, req.body.password))
await pool1Connect
try {
const request = pool1.request();
request.input('username', sql.NVarChar, req.body.username);
request.query('SELECT * FROM users WHERE username = #username', (err, result) => {
console.log(result);
bcrypt.compare(req.body.password, result.recordset[0].user_password, (err, response) => {
if (response) {
res.send(result);
} else {
res.send('wrong password')
}
})
//res.send(result)
})
} catch (err) {
res.send(err);
}
});
This code works, but when I tried to encapsulate it in a function it still didn't work.
#Anatoly mentioned .query not finishing in time which makes sense, but I thought mssql .query is an async function?
Your problem arises from an wrong assumption that callbacks and promises are alike, but on the contrary callbacks don't "respect" promise/async constructs
When the program hits the bottom of getLoginDetails the progrm execution has already split into 2 branches one branch returned you the (empty) result whereas the other one still busy with crypto operations.
Though it is true that an async function always returns a promise but that doesn't cover any future callbacks that might execute inside it. As soon as node reaches the end of function or any return statement the async function's promise get resolved(therefore future callbacks are meaningless), what you can do instead is handroll your own promise which encampasses the callbacks as well
router.post('/login', (req, res) => {
getLoginDetails(req.body.username, req.body.password))
.then((result)=>{
res.send(result);
})
.catch((err)=>{
res.send(err);
})
});
async function getLoginDetails(username, password) {
await pool1Connect
return new Promise( (resolve,reject) => {
try {
const request = pool1.request();
request.input('username', sql.NVarChar, username);
request.query('SELECT * FROM users WHERE username = #username', (err, result) => {
console.log(result);
bcrypt.compare(password, result.recordset[0].user_password, (err, response) => {
if (response) {
resolve(result);
} else {
resolve('wrong password')
}
})
})
} catch (err) {
reject(err);
}
});
}
You didn't return any result to getLoginDetails. Either you use async versions of request.query and bcrypt.compare (if any) or wrap request.query to new Promise((resolve, reject) like this:
const asyncResult = new Promise((resolve, reject) => {
request.query('SELECT ...
...
if (err) {
resolve({err: err}) // replace all return statements with resolve calls
}
...
})
const queryResult = await asyncResult;

Node.js - How do you call an async function with a callback?

The async function below is supposed to check if a url is a legit url
let CheckUrl = function (url, done) {
dns.lookup(url, function(err, address) {
if (err) return done(err);
done(null, true); //return true because I don't care what the address is, only that it works
});
}
The express.js code below gets the url but I'm having trouble understanding how to write the if statement so that it returns true or false.
// Gets URL
app.post("/api/shorturl/new", function(req, res) {
if (CheckUrl(req.body.url)) {
// do something
}
});
I'm not sure what to pass as the second argument in CheckUrl() in this if statement. Or maybe I wrote the first async function incorrectly to begin with?
Please use the async await
I have written a test code for you as below:
const express = require('express');
const app = express();
const dns = require('dns');
let CheckUrl = function (url, done) {
return new Promise((resolve, reject) => {
dns.lookup(url, function(err, address) {
console.log("err " , err)
if (err) {
resolve(false)
} else {
resolve(true)
}
});
});
}
app.post("/api/shorturl/new", async function(req, res) {
try {
let result = await CheckUrl(req.body.url);
console.log("result " , result)
res.send(result)
}
catch (error) {
console.log("in catch error " , error)
res.send(error)
}
});
app.listen(3000)
you can get the knowledge to know about the Promise here. The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
As mentioned by DeepKakkar, this was what I was looking for:
app.post("/api/shorturl/new", async (req, res) => {
try {
let result = await CheckUrl(req.body.url);
res.send(result)
}
catch (error) {
return new Error('Could not receive post');
}
});

Return call is not working in Node.js with Express

My code block for crud.js is as follows,
const listall = () => {
return client.connect(() => {
return client.invoke("ZSD_CP_PRICE_GET_ALL", {}, (err, res) => {
if (err) {
console.log('error in invoke', err);
}
console.log("ZSD_CP_PRICE_GET_ALL", res);
return res;
});
});
}
My code block for viewpage.js is as follows,
router.get('/', function(req, res) {
res.render('viewpage', {title: 'SAP', data: sapview.listall()})
})
module.exports = router;
My code block for viewpage.jade is as follows,
extends layout
block content
h1= title
p Welcome to #{title}
p Data #{data}
When I run the node application terminal logs the result like,
ZSD_CP_PRICE_GET_ALL {
IS_RETURN: {
TYPE: ''
}
But the res is never returned as I mentioned in "return res" after the console.log block in crud.js file
client.connect() is asynchronous; you have no way of getting the actual return value of whatever further asynchronous code (such as client.invoke) you call.
I suggest promisifying the invocation,
const listall = () => {
return new Promise((resolve, reject) => {
client.connect(() => {
client.invoke("ZSD_CP_PRICE_GET_ALL", {}, (err, res) => {
if (err) {
return reject(err);
}
resolve(res);
});
});
});
};
and then getting the data in an async function:
router.get("/", async (req, res) => {
const data = await sapview.listall();
res.render("viewpage", { title: "SAP", data });
});
(A further refactoring would involve a generic promisified "invoke method" function.)

How to ensure that one function completes before other function

I am fairly new to Node.js, and what I am trying to achieve is to have two separate functions. One for Auth and one for sending data (So that I don't run into rate login limits if I were to simply use a callback after conn.login finishes). I tried to set this up in node like this:
var _request = {
url: '/services/data/v45.0/actions/custom/flow/Test1',
method: 'POST',
body: JSON.stringify({
"inputs": [{}]
}),
headers: {
"Content-Type": "application/json"
}
};
var conn = new jsforce.Connection({
clientId: process.env.cliendId,
clientSecret: process.env.clientSecret,
version: "45.0"
});
function sfdcAuth() {
conn.login(process.env.sfdcUser, process.env.sfdcUserPass, (err, userInfo) => {
if (err) {
console.log(err)
}
conn = conn;
console.log("Done")
});
}
function sfdcQuery() {
conn.request(_request, function(err, resp) {
console.log(resp);
console.log(err)
});
}
sfdcAuth()
sfdcQuery()
But because js is asynchronous it runs the second function without waiting for the first function to finish.
The simplest way is to pass your second function as a callback to your first function, which it can call when it’s done:
function sfdcAuth(callback) {
conn.login(process.env.sfdcUser, process.env.sfdcUserPass, (err, userInfo) => {
if (err) {
console.log(err);
}
// Invoke callback when done
callback();
});
}
function sfdcQuery() {
conn.request(_request, function(err, resp) {
console.log(resp);
console.log(err);
});
}
// Pass second function as callback to the first
sfdcAuth(sfdcQuery);
You could also make use of promises:
function sfdcAuth(callback) {
return new Promise((resolve, reject) => {
conn.login(process.env.sfdcUser, process.env.sfdcUserPass, (err, userInfo) => {
if (err) {
reject(err);
}
resolve(userInfo);
});
});
}
function sfdcQuery() {
return new Promise((resolve, reject) => {
conn.request(_request, function(err, resp) {
if (err) {
reject(err);
}
resolve(resp);
});
});
}
// Wait for promise to resolve before invoking second function
sfdcAuth()
.then(result => {
// Do something with result
return sfdcQuery();
})
.then(result => {
// You can continue the chain with
// the result from "sfdcQuery" if you want
})
.catch(err => {
// Handle error
});

For loop issue in MEAN node jS

I am trying to send the response when the loop k value is equal to users[0].employees.length, but it's directly moving forward to max length, how do I solve it
Userlist.find(queryObj).exec(function (err, users) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
}
else {
console.log('found users list', users);
// res.json(users[0].employees);
var k=0;
var resparr=[]
for(var i=0;i<users[0].employees.length;i++){
k++;
User.find({"_id":users[0].employees[i]._id}).exec(function(err,user){
resparr=resparr.concat(user);
console.log("resparray:",k,resparr.length,i,users[0].employees.length)
if(k==users[0].employees.length)
{
console.log("success",resparr)
res.json(resparr);
}
})
}
}
});
You are using an asynchronous function call within a synchronous loop. The loop will always finish before your callback functions are called.
You will need some kind of asynchronous looping. For a native solution you can use Promises:
const getUser = (id) => {
return new Promise((resolve, reject) => {
User.find({ '_id': id })
.exec((err, user) => {
if (err) reject(err);
else resolve(user);
});
});
};
const promises = users[0].employees.map(({ _id: id }) => getUser(id));
Promise.all(promises)
.then(users => res.json(users))
.catch(err);
For utility packages in node.js, I'd recommend the package async for a callback based solution or bluebird for Promises. Both have a .map function which is perfect for this use case.

Resources