nodejs async how to set callback - node.js
I have the following piece of code which is working fine.
var config = require('./config');
var cheerio = require('cheerio');
var myhttp = require('./myHttp');
var stringHelper = require('./stringHelper');
var Base64 = require('./base64.js').Base64;
var Encrypt = require('./Encrypt.js');
var myEncode = require('./Encode.js');
var rules = require('./rules');
var io = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379 });
var mysql = require('mysql');
delete require.cache[require.resolve('./requestLogin1.js')]
var myvar = require('./requestLogin1.js');
var connection = mysql.createConnection(
{
host : 'localhost',
user : 'root',
password : 'abc',
database : 'abcd'
}
);
connection.connect(function(err) {
if (err) {
console.log('error connecting: ' + err.stack);
return;
}
});
var timerOB;
var timerMW;
var timerP;
var timerTL;
var news = {
'mw': [],
'ob': [],
'all': {},
};
var status = false;
function round(rnum, rlength) {
return newnumber = Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
}
function roundup(rnum, rlength) {
return newnumber = Math.ceil(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
}
function rounddown(rnum, rlength) {
return newnumber = Math.floor(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
}
function function1(_html) {
console.log('function1 run')
var $ = cheerio.load(_html);
var v_lgnid = $('#userId').attr('value');
var v_psswrd = config.password;
var v_data = v_lgnid + "|" + v_psswrd;
var _key = $('#accntid').attr('value');
if (_key) {
v_data = Base64.encode(Encrypt.AESEncryptCtr(v_data, _key , "256"));
v_data = escape(v_data);
myhttp.get(
'https://example.com/ValidPassword.jsp?' + $('#name').attr('value') + "=" + v_data,
function (_htmlShowImage) {
if (_htmlShowImage && _htmlShowImage.trim() == "OK") {
function2();
} else {
console.log('Login Fail');
}
});
} else {
login();
console.log('Encrypt password error');
}
}
function function2() {
myhttp.get(
'https://example.com/QuestionsAuth.jsp',
function (_htmlShowImage) {
var $ = cheerio.load(_htmlShowImage);
var sLoginID = $('#sLoginID').attr('value');
var Answer1 = config.answer1;
var Answer2 = config.answer2;
var Index1 = $('#st1').attr('value');
var Index2 = $('#st2').attr('value');
var v_data = sLoginID + "|" + Answer1 + "|" + Answer2 + "|" + Index1 + "|" + Index2;
v_data = Base64.encode(Encrypt.AESEncryptCtr(v_data, $('#key_questauth').attr('value'), "256"));
v_data = escape(v_data);
myhttp.get(
'https://example.com/ValidAnswers.jsp?' + $('#name_questauth').attr('value') + "=" + v_data,
function (_htmlShowImage) {
if (_htmlShowImage && _htmlShowImage.trim() == "OK") {
//rootCallback();
myhttp.get(
'https://example.com/DefaultLogin.jsp',
function (_html) {
console.log('Login sucess')
stringHelper.SaveFileCookies('abcd.txt', myhttp.loadCookie(), 'save cookie login sucess');
if (timerMW) {
clearTimeout(timerMW);
}
timerMW = setTimeout(function6, config.DelayExtractMW);
if (timerOB) {
clearTimeout(timerOB);
}
timerOB = setTimeout(function5, config.DelayExtractOB);
if (timerP) {
clearTimeout(timerP);
}
timerP = setTimeout(function4, config.function4);
});
} else {
console.log('Login Fail - timer');
}
});
});
}
var login = function () {
if (timerMW) {
clearTimeout(timerMW);
}
if (timerOB) {
clearTimeout(timerOB);
}
if (timerP) {
clearTimeout(timerP);
}
if (timerTL) {
clearTimeout(timerTL);
}
myhttp.init();
myhttp.post(
'https://example.com/ShowImage.jsp',
{ "requiredLogin": myEncode.Convert(config.uname) },
function (_htmlpost) {
if (_htmlpost) {
function1(_htmlpost);
} else {
if (timerTL) {
clearTimeout(timerTL);
}
timerTL = setTimeout(login, config.DelayNestError);
}
});
}
exports.login = login;
function function3() {
return {
TS: '',
MWP: 0,
LTP: 0,
NQ: 0,
OBBP: '',
OBSP: '',
CurrTime: 0,
rules: {}
};
}
function function4() {
status = false;
myhttp.get('https://example.com/PB.jsp?Exchange=',
function (_html) {
if (_html && _html.length > 10) {
news.pn = {};
$ = cheerio.load(_html);
$('tr[id^="TR"]').each(function () {
status = true;
var symbol = $('td:nth-child(3)', this).text().trim();
var objob = {
'NQ': parseInt($('td:nth-child(11)', this).text().trim()),
};
var post = {
'symbol': symbol,
'nq': objob.NQ
};
connection.query('INSERT INTO NP SET ?', post, function (err,result){
if (err)
{console.log("NP sql insert error : " +symbol);}
else {
console.log("data inserted into NP Table : " +symbol);
}
});
var objstock = news.all[symbol];
if (typeof objstock!='undefined') {
objstock.NQ = objob.NQ;
news.pn[symbol] = objob;
news.all[symbol] = objstock;
if (status) {
io.emit('news', news);
}
}
else
{
console.log('symbol not found');
}
});
if (timerP) {
clearTimeout(timerP);
}
console.log('setTimer function4:' + config.DelayExtractPn);
timerP = setTimeout(function4, config.DelayExtractPn);
}
connection.query('UPDATE MASTER1 SET tbq = (SELECT sum(a.bq) FROM (select distinct symbol, bq from NP) as a)', function (err,result){
if (err)
{console.log("CQ06 skipped: ");}
else {
console.log("Step 6 - Master1 tbq data updated");
}
});
connection.query('UPDATE MASTER1 SET tsq = (SELECT sum(a.sq) FROM (select distinct symbol, sq from NP) as a)', function (err,result){
if (err)
{console.log("CQ07 skipped: ");}
else {
console.log("Step 7 - Master1 tsq data updated");
}
});
});
}
function function5() {
status = false;
myhttp.get('https://example.com/OB.jsp?Exchange=&OrderType=All',
function (_html) {
if (_html && _html.length > 10) {
$ = cheerio.load(_html);
console.log('OB - Step 2 - html loaded for parsing');
news.ob = [];
$('tr[id^="TR"]').each(function () {
var statusOrder = $('td:nth-child(20)', this).text().trim();
if (statusOrder.toLowerCase().indexOf('open') >= 0) {
status = true;
var objob = {
'symbol': $('td:nth-child(6)', this).text().trim(),
'buysell': $('td:nth-child(9)', this).text().trim(),
'ordernumber': $('input[name="Select"]', this).attr('value'),
};
var objstock = news.all[objob.symbol];
objstock.OBBP = objob.buysell == "BUY"?objob.ordernumber:"";
objstock.OBSP = objob.buysell == "SELL"?objob.ordernumber:"";
news.ob.push(objob);
}
});
if (status) {
io.emit('news', news);
}
if (timerOB) {
clearTimeout(timerOB);
}
timerOB = setTimeout(function5, config.DelayExtractOB);
}
});
}
function function6() {
myhttp.get(
'https://example.com/MW.jsp?',
function (_html) {
if (_html && _html.length > 10) {
var $ = cheerio.load(_html);
status = false;
news.mw = [];
var countCheckRule = 0;
var countCheckedRule = 0;
var tmpall = {};
$('tr[onclick]').each(function () {
status = true;
var data1 = $("input[onclick*='Apply(']", this).attr('onclick');
var arrdata1 = data1.split("','");
var stockid = arrdata1[1].split('|')[0];
var symbol = $('td:nth-child(3)', this).text().trim();
var price = parseFloat($('td:nth-child(4)', this).text().trim());
var cTime = stringHelper.getIndiaTime();
var CurrTime = cTime.toLocaleTimeString();//(will be updated every 60 seconds)
news.mw.push({
'symbol': symbol,
'price': price,
'stockid': stockid,
'CurrTime': CurrTime,
});
var objstock = news.all[symbol];
if (!objstock) {
objstock = function3();
}
if (!news.pn[symbol]) {
objstock.NQ = 0;
}
var notfoundob = true;
for (var symbolkey in news.ob) {
if (news.ob[symbolkey].symbol == symbol) {
notfoundob = false;
}
}
if (notfoundob) {
objstock.OBBP = "";
objstock.OBSP = "";
}
objstock.TS = symbol;//trade symbol
objstock.MWP = stockid;//trade id
objstock.LTP = price;
objstock.CurrTime = CurrTime;
rules.checRules(objstock, myhttp, function (rules_res) {
objstock.rules = rules_res;
tmpall[symbol] = objstock;
});
countCheckRule++;
});
rules.rule12(tmpall, function (_tmpall) {
tmpall = _tmpall;
});
news.all = tmpall;
if (!status) {
login();
console.log('MW - Step 9 - logged out');
} else {
io.emit('news', news);
if (timerMW) {
clearTimeout(timerMW);
}
timerMW = setTimeout(function6, config.DelayExtractMW);
}
} else {
if (timerMW) {
clearTimeout(timerMW);
}
timerMW = setTimeout(function6, config.DelayExtractMW);
}
});
}
Now i want to use async to ensure that function6 is run only after function4 & function5 is run.
I have tried to learn about async from various forums and have changed the code as follows:
var async = require('async'); //line added
// change made
async.parallel([
function function4(callback) {
status = false;
myhttp.get('https://example.com/PB.jsp?Exchange=',
function (_html) {
if (_html && _html.length > 10) {
news.pn = {};
$ = cheerio.load(_html);
$('tr[id^="TR"]').each(function () {
status = true;
var symbol = $('td:nth-child(3)', this).text().trim();
var objob = {
'NQ': parseInt($('td:nth-child(11)', this).text().trim()),
};
console.log('Posn - Step 3A - Found position:' + symbol);
var post = {
'symbol': symbol,
'nq': objob.NQ
};
connection.query('INSERT INTO NP SET ?', post, function (err,result){
if (err)
{console.log("NP sql insert error : " +symbol);}
else {
console.log("data inserted into NP Table : " +symbol);
}
});
var objstock = news.all[symbol];
if (typeof objstock!='undefined') {
objstock.NQ = objob.NQ;
news.pn[symbol] = objob;
news.all[symbol] = objstock;
if (status) {
io.emit('news', news);
}
}
else
{
console.log('symbol not found');
}
});
if (timerP) {
clearTimeout(timerP);
}
console.log('setTimer function4:' + config.DelayExtractPn);
timerP = setTimeout(function4, config.DelayExtractPn);
}
connection.query('UPDATE MASTER1 SET tbq = (SELECT sum(a.bq) FROM (select distinct symbol, bq from NP) as a)', function (err,result){
if (err)
{console.log("CQ06 skipped: ");}
else {
console.log("Step 6 - Master1 tbq data updated");
}
});
connection.query('UPDATE MASTER1 SET tsq = (SELECT sum(a.sq) FROM (select distinct symbol, sq from NP) as a)', function (err,result){
if (err)
{console.log("CQ07 skipped: ");}
else {
console.log("Step 7 - Master1 tsq data updated");
}
});
callback(); //line added
});
},
function function5(callback) {
status = false;
myhttp.get('https://example.com/OB.jsp?Exchange=&OrderType=All',
function (_html) {
if (_html && _html.length > 10) {
$ = cheerio.load(_html);
console.log('OB - Step 2 - html loaded for parsing');
news.ob = [];
$('tr[id^="TR"]').each(function () {
var statusOrder = $('td:nth-child(20)', this).text().trim();
if (statusOrder.toLowerCase().indexOf('open') >= 0 || statusOrder.toLowerCase().indexOf('trigger pending') >= 0) {
status = true;
var objob = {
'symbol': $('td:nth-child(6)', this).text().trim(),
'buysell': $('td:nth-child(9)', this).text().trim(),
'ordernumber': $('input[name="Select"]', this).attr('value'),
};
var objstock = news.all[objob.symbol];
objstock.OBBP = objob.buysell == "BUY"?objob.ordernumber:"";
objstock.OBSP = objob.buysell == "SELL"?objob.ordernumber:"";
news.ob.push(objob);
}
});
if (status) {
console.log('OB - Step 5 - pushed to html page');
io.emit('news', news);
}
if (timerOB) {
clearTimeout(timerOB);
}
timerOB = setTimeout(function5, config.DelayExtractOB);
}
callback(); //line added
});
}
],
function function6() {
myhttp.get(
'https://example.com/MW.jsp?',
function (_html) {
if (_html && _html.length > 10) {
var $ = cheerio.load(_html);
status = false;
news.mw = [];
var countCheckRule = 0;
var countCheckedRule = 0;
var tmpall = {};
$('tr[onclick]').each(function () {
status = true;
var data1 = $("input[onclick*='Apply(']", this).attr('onclick');
var arrdata1 = data1.split("','");
var stockid = arrdata1[1].split('|')[0];
var symbol = $('td:nth-child(3)', this).text().trim();
var price = parseFloat($('td:nth-child(4)', this).text().trim());
var cTime = stringHelper.getIndiaTime();
var CurrTime = cTime.toLocaleTimeString();//(will be updated every 60 seconds)
news.mw.push({
'symbol': symbol,
'price': price,
'stockid': stockid,
'CurrTime': CurrTime,
});
var objstock = news.all[symbol];
if (!objstock) {
objstock = function3();
}
if (!news.pn[symbol]) {
objstock.NQ = 0;
}
var notfoundob = true;
for (var symbolkey in news.ob) {
if (news.ob[symbolkey].symbol == symbol) {
notfoundob = false;
}
}
if (notfoundob) {
objstock.OBBP = "";
objstock.OBSP = "";
}
objstock.TS = symbol;//trade symbol
objstock.MWP = stockid;//trade id
objstock.LTP = price;
objstock.CurrTime = CurrTime;
rules.checRules(objstock, myhttp, function (rules_res) {
objstock.rules = rules_res;
tmpall[symbol] = objstock;
});
countCheckRule++;
});
//new check rules
rules.rule12(tmpall, function (_tmpall) {
tmpall = _tmpall;
});
news.all = tmpall;
if (!status) {
login();
} else {
io.emit('news', news);
if (timerMW) {
clearTimeout(timerMW);
}
timerMW = setTimeout(function6, config.DelayExtractMW);
}
} else {
if (timerMW) {
clearTimeout(timerMW);
}
timerMW = setTimeout(function6, config.DelayExtractMW);
}
});
});
Since my original functions do not have any callback, and since async needs callback, i have tried to code a callback in function4 & function5 - but I guess i have not coded it correctly.
I am getting an error in the line where callback is present that states "TypeError: undefined is not a function".
How do i correct this?
Related Question No. 2 : the current code has a timer function whereby function4, function5 and function6 runs with a preset timer. If async works, how do i define a timer whereby the combined set of function4,5 & 6 works based on a preset timer?
Sorry about the long code -- i am new to nodejs and was handed over this code as such and am trying to get this change made.
Thanks for your guidance.
You can instead make function4 and function5 to return promise and then execute function6 only after both function4's and function5's promise gets resolved.
Related
Array push gives empty result node js
I am creating an API for listing trip data with image and pdf base url, All things are working fine but I can not access the last result array data_to_send out of for loop. app.js app.get("/getChallanList/:userId/:role", (req, res) => { const userData = req.params; let site_source = ""; let site_destination = ""; var site_from_name = ""; const data_to_send = []; if (userData.role == "D") { db.select("trip", "*", `driver_id = '${req.params.userId}'`, (data) => { for (let i = 0; i < data.data.length; i++) { site_source = data.data[i].site_from; site_destination = data.data[i].site_to; db.select( "site", "*", `id in ('${site_source}','${site_destination}')`, (data_site) => { data.data[i].site_from = data_site.data[0].name; data.data[i].site_to = data_site.data[1].name; if (data.data[i].truck_challan_pdf != "") { data.data[i].truck_challan_pdf = base_url + "truckchallan/" + data.data[i].truck_challan_pdf; } if (data.data[i].driver_challan_pdf != "") { data.data[i].driver_challan_pdf = base_url + "driverchallan/" + data.data[i].driver_challan_pdf; } if (data.data[i].preparer_img != "") { data.data[i].preparer_img = base_url + data.data[i].preparer_img; } if (data.data[i].driver_img != "") { data.data[i].driver_img = base_url + data.data[i].driver_img; } data_to_send.push(data.data); // console.log(data_to_send); // working } ); } console.log(data_to_send); // empty }); } } db.select let select = (table, column, condition, callback) => { try { let sql = "SELECT " + column + " FROM " + table + " WHERE " + condition; conn.query(sql, (err, results) => { if (err) { let data = { status: 0, data: sql, message: "Something went wrong!", }; callback(data); } else { let data = { status: 1, data: results, message: "Success", }; callback(data); } }); } catch (err) { let data = { status: 0, data: err, message: "In catch", }; callback(data); } }; async await app.get("/getChallanList/:userId/:role", async (req, res) => { const userData = req.params; let site_source = ""; let site_destination = ""; var site_from_name = ""; const data_to_send = []; if (userData.role == "D") { await db.select( "trip", "*", `driver_id = '${req.params.userId}'`, async (data) => { // const data_to_send_ = []; for (let i = 0; i < data.data.length; i++) { site_source = data.data[i].site_from; site_destination = data.data[i].site_to; await db.select( "site", "*", `id in ('${site_source}','${site_destination}')`, (data_site) => { data.data[i].site_from = data_site.data[0].name; data.data[i].site_to = data_site.data[1].name; if (data.data[i].truck_challan_pdf != "") { data.data[i].truck_challan_pdf = base_url + "truckchallan/" + data.data[i].truck_challan_pdf; } if (data.data[i].driver_challan_pdf != "") { data.data[i].driver_challan_pdf = base_url + "driverchallan/" + data.data[i].driver_challan_pdf; } if (data.data[i].preparer_img != "") { data.data[i].preparer_img = base_url + data.data[i].preparer_img; } if (data.data[i].driver_img != "") { data.data[i].driver_img = base_url + data.data[i].driver_img; } data_to_send.push(data.data); // console.log(data_to_send); // working } ); // data_to_send_.push(data_to_send); } console.log(data_to_send); // empty } ); } }
this is because of the asynchronous behavior of NodeJs, so you have to plan things accordingly i.e console.log(1) db.select( "trip", "*", `driver_id = '${req.params.userId}'`, async (data) => { console.log(2) }) console.log(3) The output of the above code would be 1 then 3 and then 2 and this is how NodeJs works it does not wait for I/O calls i.e DB query in your case. Please check how promises work in NodeJs for more details. Here is how you can accomplish your task: const challanList = (userData) => { return new Promise((resolve, reject) => { const data_to_send = []; db.select("trip", "*", `driver_id = '${req.params.userId}'`, data => { for (let i = 0; i < data.data.length; i++) { const site_source = data.data[i].site_from; const site_destination = data.data[i].site_to; db.select("site", "*", `id in ('${site_source}','${site_destination}')`, data_site => { data.data[i].site_from = data_site.data[0].name; data.data[i].site_to = data_site.data[1].name; if (data.data[i].truck_challan_pdf != "") { data.data[i].truck_challan_pdf = base_url + "truckchallan/" + data.data[i].truck_challan_pdf; } if (data.data[i].driver_challan_pdf != "") { data.data[i].driver_challan_pdf = base_url + "driverchallan/" + data.data[i].driver_challan_pdf; } if (data.data[i].preparer_img != "") { data.data[i].preparer_img = base_url + data.data[i].preparer_img; } if (data.data[i].driver_img != "") { data.data[i].driver_img = base_url + data.data[i].driver_img; } data_to_send.push(data.data); // console.log(data_to_send); // working }); } resolve(data_to_send); }); }); }; app.get("/getChallanList/:userId/:role", async (req, res) => { const userData = req.params; const challanListResult =await challanList(userData); console.log(challanListResult); resp.json(challanListResult); });
Without knowing what database or ORM you are using it is difficult to answer, but my suspicion is that db.select is an asynchronous method, i.e. it is returning a Promise. If so, the second console log is still seeing the "old" data_to_send. Try adding an await in front of the first db.select call. (Don't forget the async in front of the callback in second argument of app.get.
Your database is asynchronous so console.log(data_to_send) gets called before the query finished executing. Try adding async before (req, res) in line 1 then await before db.select.
This works for me app.get("/getChallanList/:userId/:role", async (req, res) => { const userData = req.params; let site_source = ""; let site_destination = ""; var site_from_name = ""; const data_to_send = []; if (userData.role == "D") { const data = await db.query( `SELECT * FROM trip WHERE driver_id = '${req.params.userId}'` ); // console.log(data.length); // const data_to_send_ = []; for (let i = 0; i < data.length; i++) { site_source = data[i].site_from; site_destination = data[i].site_to; // cons const site_data = await db.query( `SELECT * FROM site WHERE id in ('${site_source}','${site_destination}')` ); // console.log(site_data); db.select( "site", "*", `id in ('${site_source}','${site_destination}')`, (data_site) => { data[i].site_from = data_site.data[0].name; data[i].site_to = data_site.data[1].name; if (data[i].truck_challan_pdf != "") { data[i].truck_challan_pdf = base_url + "truckchallan/" + data[i].truck_challan_pdf; } if (data[i].driver_challan_pdf != "") { data[i].driver_challan_pdf = base_url + "driverchallan/" + data[i].driver_challan_pdf; } if (data[i].preparer_img != "") { data[i].preparer_img = base_url + data[i].preparer_img; } if (data[i].driver_img != "") { data[i].driver_img = base_url + data[i].driver_img; } data_to_send.push(data); // console.log(data.data); // console.log(data_to_send); // working } ); // data_to_send_.push(data_to_send); } // console.log(data_to_send); // console.log(data_to_send); res.send({ success: 1, data: data, message: "" }); }
Getting Transaction Failed Error: No valid responses from any peer
The error I got: enrollSTudent Transaction Failed: text error trying invoke business network.Error: No valid responses from any peers. Response from attempted peer comms was an error: Error: transaction returned with failure: ReferenceError: NS is not defined. I also got an other error saying there is a memory leak but fixed it. However, i cant fix NS_ is undefined error. Here is the code i used below: Chaincode.js Chaincode.js use strict; /** * Write your transction processor functions here */ var NS_UNIVERSITY = 'org.gryphon.casestudy.university'; var NS_COLLEGE = 'org.gryphon.casestudy.college'; var NS_STUDENT = 'org.gryphon.casestudy.student'; /** * create a new certificate entry * #param {org.gryphon.casestudy.university.issueCertificate} args - student details * #transaction */ function issueCertificate(args) { var certificateId = 'CertificateID-' + Date.now().toString(); var certificate; var _assetRegistry; return getAssetRegistry(NS_UNIVERSITY + '.Certificate') .then(function (assetRegistry) { var factory = getFactory(); certificate = factory.newResource(NS_UNIVERSITY, 'Certificate', certificateId); certificate.issuedTo = args.studentName; certificate.programName = args.programName; certificate.issuedDate = new Date(); certificate.certificateId = certificateId; return assetRegistry.add(certificate) .then(function (_res) { return getParticipantRegistry(NS_STUDENT + '.Student'); }) .then(function (assetRegistry) { _assetRegistry = assetRegistry; return assetRegistry.get(args.studentID) .then(function (certifiedStudent) { certifiedStudent.certificateId = certificateId; var event = factory.newEvent(NS_UNIVERSITY, 'certificateIssuedEvent'); event.certificateId = certificateId; emit(event); return _assetRegistry.update(certifiedStudent) }) .then(function (_res) { return (_res); }).catch( function (error) { return (error); }); }); }); } /** * approve affiliation of college * #param {org.gryphon.casestudy.university.approveAffiliation} args - id of college * #transaction */ function approveAffiliation(args) { var registry; return getParticipantRegistry(NS_COLLEGE + '.College') .then(function (assetRegistry) { registry = assetRegistry; return assetRegistry.get(args.memberId) .then(function (college) { college.isApproved = 1; return registry.update(college) .then(function (_res) { return (_res); }).catch( function (error) { return (error); }) }); }); } /** * Enroll Program to the college * #param {org.gryphon.casestudy.college.enrollProgram} args - id of college * #transaction */ function enrollProgram(args) { var registry; return getParticipantRegistry(NS_COLLEGE + '.College') .then(function (assetRegistry) { registry = assetRegistry; return assetRegistry.get(args.collegeId) .then(function (college) { if (college.isApproved == 1) { college.programs.push(args.programName) } return registry.update(college) .then(function (_res) { console.log(_res) return (_res); }).catch( function (error) { return (error); }) }); }); } /** * Student can enroll them to a College and Program * #param {org.gryphon.casestudy.student.enrollStudent} args - student details * #transaction */ function enrollStudent(args) { return getParticipantRegistry(NS_STUDENT + '.Student') .then(function (assetRegistry) { var factory = getFactory(); var studentId = 'Student-' + Date.now().toString(); var student = factory.newResource(NS_STUDENT, 'Student', studentId); student.name = args.name; student.dob = args.dob; student.memberId = studentId; student.programName = args.programName; student.collegeName = args.collegeName; return assetRegistry.add(student) .then(function (_res) { return (_res); }).catch( function (error) { return (error); }); }); } /** * College can request Affiliation to the University * #param {org.gryphon.casestudy.college.requestAffiliation} args - name of college * #transaction */ function requestAffiliation(args) { return getParticipantRegistry(NS_COLLEGE + '.College') .then(function (assetRegistry) { var factory = getFactory(); var collegeId = 'College-' + Date.now().toString(); var college = factory.newResource(NS_COLLEGE, 'College', collegeId); college.name = args.name; college.memberId = collegeId; college.isApproved = 0; college.programs = []; return assetRegistry.add(college) .then(function (_res) { return (_res); }).catch( function (error) { return (error); }); }); } events.js file: function requestAffiliation() { // Show Progress untill the task is complete document.getElementById('college_progress').innerHTML = '<img src="./icons/progress.gif"/>'; // Get the attributes from the UI i.e College Name let college_name = document.getElementById('college_name').value; // Set the Payload for the Post restapi call let options = { 'college_name': college_name }; { // Use Async ajax call to post a request to the Client App $.when($.post('/composer/client/requestAffiliation', options)).done(function (_res) { // End progress display document.getElementById('college_progress').innerHTML = ''; // Update the UI with college list displayCollegeList(); // Close the Modal Dialog closeModal(); }); } } /** * Display Colleges */ function displayCollegeList() { // Use Async ajax call to get a list of colleges from the Client App $.when($.get('composer/client/getCollegeList')).done(function (_res) { // Successful response received, lets check it on browser console console.log(_res.college_list); let _str = ''; let _nstr = ''; // We will now build the Table to be displayed in UI _str += '<table><tr><th>Name</th><th>isApproved</th></tr>'; _res.college_list.forEach(function (_row) { // Check is college is approved or NOT aaproved by university let td = (_row.is_approved == 0) ? '<td class="red"> NOT APPROVED</td>' : '<td class="green"> APPROVED</td>'; _str += '<tr><td>' + '<a href="#" onClick=enrollProgram("' + _row.id + '")>' + _row.name + '</a></td>' + td + '</tr>'; if (_row.is_approved == 0) { _nstr += '<input type="checkbox" name="collegeIds" value="' + _row.id + '">' + _row.name + '</input></br>'; } }) _str += '</table>'; // Display College List document.getElementById('college_list').innerHTML = _str; document.getElementById('approve_list').innerHTML = _nstr; }); } function submitApproveAffiliation() { let options = {}; let arr = ''; document.getElementById('approval_progress').innerHTML = '<img src="./icons/progress.gif"/>'; $("input:checkbox[name=collegeIds]:checked").each(function () { let options = {}; options.college_id = $(this).val(); { $.when($.post('/composer/client/approveAffiliation', options)).done(function (_res) { document.getElementById('approval_progress').innerHTML = ''; let val = _res.result; displayCollegeList(); closeModal(); }); } }); } function submitEnrollProgram() { document.getElementById('program_progress').innerHTML = '<img src="./icons/progress.gif"/>'; let options = {}; options.program_name = document.getElementById('program_name').value; options.college_id = collegeId; { $.when($.post('/composer/client/enrollProgram', options)).done(function (_res) { document.getElementById('program_progress').innerHTML = ''; let val = _res.result; displayCollegeList(); closeModal(); }); } } function submitGenerateCertificate() { let options = {}; let arr = ''; document.getElementById('certificate_progress').innerHTML = '<img src="./icons/progress.gif"/>'; $("input:checkbox[name=gen_cer_student]:checked").each(function () { let options = {}; options.student_id = $(this).val(); { $.when($.post('/composer/client/issueCertificate', options)).done(function (_res) { document.getElementById('certificate_progress').innerHTML = ''; let val = _res.result; displayStudentList(); closeModal(); }); } }); } function submitTakeAdmission() { var selected_program = $("input[name=program]:checked").val(); if (selected_program == null) { alert('No Program Selected'); return; } let student_name = document.getElementById('student_name').value; let student_dob = document.getElementById('student_dob').value; let studentDob = new Date(student_dob); if (studentDob == null) { alert('Incorrect Date Format'); return; } let val = selected_program.split('##'); let arr = ''; document.getElementById('admission_progress').innerHTML = '<img src="./icons/progress.gif"/>'; let options = {}; options.student_name = student_name; options.student_dob = studentDob; options.college_name = val[0]; options.program_name = val[1]; { $.when($.post('/composer/client/takeAdmission', options)).done(function (_res) { document.getElementById('admission_progress').innerHTML = ''; let val = _res.result; displayStudentList(); closeModal(); }); } } /** * get History */ function getHistorian() { $.when($.get('fabric/getHistory')).done(function (_res) { let _str = '<h4> HyperLedger Transaction Blocks: ' + _res.result + '</h4>'; if (_res.result === 'success') { _str += '<h3>Total Blocks: ' + _res.history.length + '</h3>'; _str += '<table id="tt"><tr><th>Transaction Hash</th><th>Transaction Type</th><th>TimeStamp</th></tr>'; _res.history.sort(function (a, b) { return (b.transactionTimestamp > a.transactionTimestamp) ? -1 : 1; }); for (let each in _res.history) { (function (_idx, _arr) { let _row = _arr[_idx]; _str += '<tr><td>' + _row.transactionId + '</td><td>' + _row.transactionType + '</td><td>' + _row.transactionTimestamp + '</td></tr>'; })(each, _res.history); } _str += '</table>'; } else { _str += formatMessage(_res.message); } document.getElementById('historian').innerHTML = _str; }); } /** * Display Students */ function displayStudentList() { $.when($.get('composer/client/getStudentList')).done(function (_res) { console.log(_res.student_list); let _str = ''; let _nstr = ''; _str += '<table><tr><th>Name</th><th>Certificate ID</th></tr>'; _res.student_list.forEach(function (_row) { let cid = (_row.certificateId == null) ? '...' : _row.certificateId; _str += '<tr><td>' + _row.name + '</td><td>' + cid + '</td></tr>'; }) _str += '</table>'; document.getElementById('student_list').innerHTML = _str; }); } let collegeId = null; function enrollProgram(id) { collegeId = id; var appendthis = ("<div class='modal-overlay js-modal-close'></div>"); $("body").append(appendthis); $(".modal-overlay").fadeTo(500, 0.7); var modalBox = $(this).attr('data-modal-id'); $('#' + 'enrollProgram').fadeIn($(this).data()); } function approveAffiliation() { var appendthis = ("<div class='modal-overlay js-modal-close'></div>"); $("body").append(appendthis); $(".modal-overlay").fadeTo(500, 0.7); var modalBox = $(this).attr('data-modal-id'); $('#' + 'approveAffiliation').fadeIn($(this).data()); } function takeAdmission() { console.log('takeAdmission......'); $.when($.get('composer/client/getCollegeList')).done(function (_res) { let _str = ''; _str += '<table><tr><th>College</th><th>Programs</th></tr>'; _res.college_list.forEach(function (_row) { if (_row.is_approved == 1) { let _pro_str = '<table>'; _row.programs.forEach(function (_pro) { _pro_str += '<tr><td><input type="radio" name="program" value="' + _row.name + '##' + _pro + '">' + _pro + '</input></td></tr>'; }) _str += '<tr class="mark"><td>' + _row.name + '</td><td>' + _pro_str + '</table></td>' + '</tr>'; } }) _str += '</table>'; document.getElementById('admission_list').innerHTML = _str; }); var appendthis = ("<div class='modal-overlay js-modal-close'></div>"); $("body").append(appendthis); $(".modal-overlay").fadeTo(500, 0.7); var modalBox = $(this).attr('data-modal-id'); $('#' + 'takeAdmission').fadeIn($(this).data()); } function generateCertificate() { console.log('generateCertificate......'); $.when($.get('composer/client/getStudentList')).done(function (_res) { let _str = ''; _str += '<table><tr><th>Select</th><th>Student ID</th><th>Student Name</th></tr>'; _res.student_list.forEach(function (_row) { if (_row.certificateId == null) { _str += '<tr><td><input type="checkbox" name="gen_cer_student" value="' + _row.id + '"></input></td><td>' + _row.id + '</td><td>' + _row.name + '</td></tr>'; } }) _str += '</table>'; document.getElementById('student_cer_list').innerHTML = _str; }); var appendthis = ("<div class='modal-overlay js-modal-close'></div>"); $("body").append(appendthis); $(".modal-overlay").fadeTo(500, 0.7); var modalBox = $(this).attr('data-modal-id'); $('#' + 'generateCertificate').fadeIn($(this).data()); } function verifyCertificate() { let public_id = document.getElementById('verify_cer_id').value; if (public_id == '') { alert('Please Enter valid Public ID'); return; } /* let options = { id: public_id }; document.getElementById('verify_progress').innerHTML = '<img src="./icons/progress.gif"/>'; $.when($.get('composer/client/getCertificateById', options)).done(function (_res) { console.log(res.certificates); }); */ document.getElementById('verify_progress').innerHTML = '<img src="./icons/progress.gif"/>'; $.when($.get('composer/client/getStudentList')).done(function (_res) { let list = _res.student_list; var result = null; list.forEach(function (v) { console.log(v); if (v.certificateId == public_id) { result = v; } }); console.log(result); if (result == null) { alert('Certificate Validation Failed - No such certificate found!'); closeModal(); return; } document.getElementById('_student_name_').innerHTML = result.name; document.getElementById('_program_name_').innerHTML = result.program; document.getElementById('_date_of_issue').innerHTML = Date().toString().split('2018')[0] + '2018'; document.getElementById('__cer_id__').innerHTML = public_id; document.getElementById('verify_progress').innerHTML = ''; closeModal(); displayCertificate(); }); } function displayCertificate() { var appendthis = ("<div class='modal-overlay js-modal-close'></div>"); $("body").append(appendthis); $(".modal-overlay").fadeTo(500, 0.7); var modalBox = $(this).attr('data-modal-id'); $('#' + 'certificate').fadeIn($(this).data()); } This is mostly the working code for my hyper ledger. As i said the error is NS_ is not defined but i clearly checked all my code and found that i never mistyped NS_
this works for me (ps. should reflect the participant registry naming ('stuRegistry' rather than being an 'assetRegistry' but that still works anyway FYI) - not sure if you provided (judging by the error msg above) an invalid namespace in the transaction class? FYI my namespaces are defined as : ... var NS_UNIVERSITY = 'org.gryphon.casestudy.university'; var NS_COLLEGE = 'org.gryphon.casestudy.college'; var NS_STUDENT = 'org.gryphon.casestudy.student'; ... /** * Student can enroll them to a College and Program * #param {org.gryphon.casestudy.student.enrollStudent} args - student details * #transaction */ function enrollStudent(args) { return getParticipantRegistry(NS_STUDENT + '.Student') .then(function (stuRegistry) { var factory = getFactory(); var studentId = 'Student-' + + Date.parse(args.timestamp); // removed this non deterministic code: Date.now().toString(); var student = factory.newResource(NS_STUDENT, 'Student', studentId); student.name = args.name; student.dob = args.dob; student.memberId = studentId; student.programName = args.programName; student.collegeName = args.collegeName; return stuRegistry.add(student) .then(function (_res) { return (_res); }).catch( function (error) { return (error); }); }); } for issueCertificate, here's an example written with async / await as its easier/simpler and use the transaction (args) timestamp field to generate your id deterministically (not new Date()): /** * Write your transcation processor functions here etc */ var NS_UNIVERSITY = 'org.gryphon.casestudy.university'; var NS_COLLEGE = 'org.gryphon.casestudy.college'; var NS_STUDENT = 'org.gryphon.casestudy.student'; /** * create a new certificate entry * #param {org.gryphon.casestudy.university.issueCertificate} args - student details * #transaction */ async function issueCertificate(args) { // `Date.now().toString();` is non-deterministic var certificateId = 'CertificateID-' + Date.parse(args.timestamp); var factory = getFactory(); const assetRegistry = await getAssetRegistry(NS_UNIVERSITY + '.Certificate'); const certificate = factory.newResource(NS_UNIVERSITY, 'Certificate', certificateId); certificate.issuedTo = args.studentName; certificate.programName = args.programName; certificate.issuedDate = new Date(); certificate.certificateId = certificateId; await assetRegistry.add(certificate) ; const participantRegistry = await getParticipantRegistry(NS_STUDENT + '.Student'); const certifiedStudent = await participantRegistry.get(args.studentID) certifiedStudent.certificateId = certificateId; var event = factory.newEvent(NS_UNIVERSITY, 'certificateIssuedEvent'); event.certificateId = certificateId; emit(event); participantRegistry.update(certifiedStudent); }
Network Error 12030 using Websockets
Hello there i am integrating Wowza Media Server with Temasys plugin(for crossbrowser support) and the code runs great on Chrome but on IE11+ its is giving me network error.Please see attached image and code for details. const GO_BUTTON_START = "Publish"; const GO_BUTTON_STOP = "Stop"; var video = document.querySelector('video'); var peerConnection = null; var peerConnectionConfig = {'iceServers': []}; var localStream = null; var wsURL = "wss://localhost.streamlock.net/webrtc-session.json"; var wsConnection = null; var streamInfo = {applicationName:"webrtc", streamName:"myStream", sessionId:"[empty]"}; var userData = {param1:"value1"}; var videoBitrate = 360; var audioBitrate = 64; var newAPI = false; navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia; window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; window.RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate || window.webkitRTCIceCandidate; window.RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription; //var constraints = { //audio: false, //video: true //}; function successCallback(stream) { window.stream = stream; // make stream available to browser console localStream=stream; console.log(localStream); video = attachMediaStream(video, stream); } function errorCallback(error) { console.log('navigator.getUserMedia error: ', error); } function pageReady() { $("#buttonGo").attr('value', GO_BUTTON_START); var constraints = { video: true, audio: false, }; //alert('here'); navigator.getUserMedia(constraints, successCallback, errorCallback); console.log("newAPI: "+newAPI); } function wsConnect(url) { wsConnection = new WebSocket(url); wsConnection.binaryType = 'arraybuffer'; wsConnection.onopen = function() { console.log("wsConnection.onopen"); peerConnection = new RTCPeerConnection(peerConnectionConfig); peerConnection.onicecandidate = gotIceCandidate; if (newAPI) { var localTracks = localStream.getTracks(); for(localTrack in localTracks) { console.log("video stream"+localStream); peerConnection.addTrack(localTracks[localTrack], localStream); } } else { console.log("video stream"+localStream); peerConnection.addStream(localStream); } peerConnection.createOffer(gotDescription, errorHandler); } wsConnection.onmessage = function(evt) { console.log("wsConnection.onmessage: "+evt.data); var msgJSON = JSON.parse(evt.data); var msgStatus = Number(msgJSON['status']); var msgCommand = msgJSON['command']; if (msgStatus != 200) { $("#sdpDataTag").html(msgJSON['statusDescription']); stopPublisher(); } else { $("#sdpDataTag").html(""); var sdpData = msgJSON['sdp']; if (sdpData !== undefined) { console.log('sdp: '+msgJSON['sdp']); peerConnection.setRemoteDescription(new RTCSessionDescription(sdpData), function() { //peerConnection.createAnswer(gotDescription, errorHandler); }, errorHandler); } var iceCandidates = msgJSON['iceCandidates']; if (iceCandidates !== undefined) { for(var index in iceCandidates) { console.log('iceCandidates: '+iceCandidates[index]); peerConnection.addIceCandidate(new RTCIceCandidate(iceCandidates[index])); } } } if (wsConnection != null) wsConnection.close(); wsConnection = null; } wsConnection.onclose = function(error) { console.log("wsConnection.onclose"+error); } wsConnection.onerror = function(evt) { console.log("wsConnection.onerror: "+JSON.stringify(evt)); $("#sdpDataTag").html('WebSocket connection failed: '+wsURL); stopPublisher(); } } function startPublisher() { wsURL = $('#sdpURL').val(); streamInfo.applicationName = $('#applicationName').val(); streamInfo.streamName = $('#streamName').val(); videoBitrate = $('#videoBitrate').val(); audioBitrate = $('#audioBitrate').val(); $.cookie("webrtcPublishWSURL", wsURL, { expires: 365 }); $.cookie("webrtcPublishApplicationName", streamInfo.applicationName, { expires: 365 }); $.cookie("webrtcPublishStreamName", streamInfo.streamName, { expires: 365 }); $.cookie("webrtcPublishVideoBitrate", videoBitrate, { expires: 365 }); $.cookie("webrtcPublishAudioBitrate", audioBitrate, { expires: 365 }); console.log("startPublisher: wsURL:"+wsURL+" streamInfo:"+JSON.stringify(streamInfo)); wsConnect(wsURL); $("#buttonGo").attr('value', GO_BUTTON_STOP); } function stopPublisher() { if (peerConnection != null) peerConnection.close(); peerConnection = null; if (wsConnection != null) wsConnection.close(); wsConnection = null; $("#buttonGo").attr('value', GO_BUTTON_START); console.log("stopPublisher"); } function btn_start() { alert('button clicked'); if (peerConnection == null) startPublisher(); else stopPublisher(); } function gotIceCandidate(event) { if(event.candidate != null) { //console.log('gotIceCandidate: '+JSON.stringify({'ice': event.candidate})); } } function gotDescription(description) { var enhanceData = new Object(); if (audioBitrate !== undefined) enhanceData.audioBitrate = Number(audioBitrate); if (videoBitrate !== undefined) enhanceData.videoBitrate = Number(videoBitrate); description.sdp = enhanceSDP(description.sdp, enhanceData); console.log('gotDescription: '+JSON.stringify({'sdp': description})); peerConnection.setLocalDescription(description, function () { wsConnection.send('{"direction":"publish", "command":"sendOffer", "streamInfo":'+JSON.stringify(streamInfo)+', "sdp":'+JSON.stringify(description)+', "userData":'+JSON.stringify(userData)+'}'); console.log("stream info"+JSON.stringify(streamInfo)); console.log("stream description"+JSON.stringify(description)); console.log("stream userData"+JSON.stringify(userData)); }, function() {console.log('set description error')}); } function enhanceSDP(sdpStr, enhanceData) { var sdpLines = sdpStr.split(/\r\n/); var sdpSection = 'header'; var hitMID = false; var sdpStrRet = ''; for(var sdpIndex in sdpLines) { var sdpLine = sdpLines[sdpIndex]; if (sdpLine.length <= 0) continue; sdpStrRet += sdpLine+'\r\n'; if (sdpLine.indexOf("m=audio") === 0) { sdpSection = 'audio'; hitMID = false; } else if (sdpLine.indexOf("m=video") === 0) { sdpSection = 'video'; hitMID = false; } if (sdpLine.indexOf("a=mid:") === 0) { if (!hitMID) { if ('audio'.localeCompare(sdpSection) == 0) { if (enhanceData.audioBitrate !== undefined) { sdpStrRet += 'b=AS:' + enhanceData.audioBitrate + '\r\n'; sdpStrRet += 'b=TIAS:' + (enhanceData.audioBitrate*1024) + '\r\n'; } } else if ('video'.localeCompare(sdpSection) == 0) { if (enhanceData.videoBitrate !== undefined) { sdpStrRet += 'b=AS:' + enhanceData.videoBitrate + '\r\n'; sdpStrRet += 'b=TIAS:' + (enhanceData.videoBitrate*1024) + '\r\n'; } } hitMID = true; } } } return sdpStrRet; } function errorHandler(error) { console.log(error); } Click here to see image
How to handle the node server side with react
Im working on react and node project. Im new to both technologies, and i developed a system for my internship. But I feel like, I didn't handle the server side (Node part) properly. This is my server side file. It container almost 700 lines. Do I have to break this page in to several pages? If so, how exactly I should do it? Can someone give me a suggestion please. The application is working as expected. I just want to clean the code in the server side. I used express framework with node. var express = require('express'); var mysql = require('mysql'); var _ = require('underscore'); var crypto = require('crypto'); var app = express(); var connections = []; var title = 'Fishery Logistics'; var flds = []; var currentFld = ''; var fldDetails = []; var lfrSaved = false; var userSiteInfoSaved = false; var userList = []; var userTypes = []; var validUserName = false; var fldNumbers = []; var productCodes = []; var containerTypes = []; var areaCodes = []; var fldRows = 0; var fldInfo = {}; var productInfo = {}; var weighInList = {}; var weighInSaved = false; var weighInNumbers = []; var weighInWeights = []; var fldHeaderInfo = []; var weighInHeaderInfo = []; var fldWeights = []; var userDeleted = false; // From where express should access our files app.use(express.static('./public')); app.use(express.static('./node_modules/bootstrap/dist')); var server = app.listen(process.env.PORT || 3000); // Creating a socket server which is also listeing to localhost:<port> var io = require('socket.io').listen(server); var connection = mysql.createConnection({ host: "localhost", user: "root", password: '', database: 'fish_landing' }); io.sockets.on('connection', function(socket) { // Load fld list relevant to a LFR socket.on('lfrFldListLoad', function(payload) { var lfr_id = payload.lfrId; var sql_lfr_fld_list = 'SELECT F.fld_id, F.fld_number, DATE_FORMAT(F.landed_date, "%Y-%m-%d") AS "landed_date", F.vessel_name, F.port_name, F.transport_company_name, F.driver_name, truck_number, FS.status ' + 'FROM fld F, fld_status FS, lfr L ' + 'WHERE F.status_id = FS.status_id ' + 'AND F.lfr_id = L.lfr_id ' + 'AND L.lfr_id = ' + lfr_id + ' ORDER BY F.fld_id DESC'; connection.query(sql_lfr_fld_list, function(error, result) { if (error) { console.log(error); } if (result.length !== 0) { flds = result; io.sockets.emit('lfrFldListLoaded', flds); } else { console.log('No Records Found') } }); }); // Load fld with all the details socket.on('fldViewLoad', function(payload) { var fld_id = payload.fldId; var sql_fld_by_id = 'SELECT F.fld_id, F.fld_number, DATE_FORMAT(F.landed_date, "%Y-%m-%d") AS "landed_date", DATE_FORMAT(F.landed_date, "%T") AS "landed_time", ' + 'F.port_name, F.vessel_name, F.skipper_name, F.transport_company_name, F.truck_number, F.driver_name, F.driver_license, F.vehicle_clean, ' + 'F.vehicle_refrigerated, F.containers_clean, F.containers_iced, F.skipper_signature, F.supervisor_signature, F.lfr_staff_signature, ' + 'F.skipper_signature_time, F.supervisor_signature_time, F.lfr_staff_signature_time, F.comment, FS.status, CONCAT(U.first_name, " ", U.last_name) AS "full_name", ' + 'DATE_FORMAT(F.fld_created_time, "%Y-%m-%d") AS "fld_created_date", DATE_FORMAT(F.fld_created_time, "%T") AS "fld_created_time" ' + 'FROM fld F, fld_status FS, user_personal_info U ' + 'WHERE F.status_id = FS.status_id ' + 'AND F.fld_created_user_id = U.user_id ' + 'AND F.fld_id = "' + fld_id + '"'; var sql_fld_detail_list = 'SELECT FD.fld_detail_id, SP.species, PS.state, C.container, FD.no_of_containers, FD.fld_id ' + 'FROM fld_details FD, species SP, processed_state PS, container C, fld F ' + 'WHERE F.fld_id = FD.fld_id ' + 'AND SP.species_id = FD.species_id ' + 'AND PS.state_id = FD.state_id ' + 'AND C.container_id = FD.container_id ' + 'AND F.fld_id = "' + fld_id + '"'; connection.query(sql_fld_by_id, function(errorFld, resultFld) { if (errorFld) { console.log(errorFld); } if (resultFld.length !== 0) { currentFld = resultFld; connection.query(sql_fld_detail_list, function(errorFldDetails, resultFldDetails) { if (errorFldDetails) { console.log(errorFldDetails); } else { fldDetails = resultFldDetails; io.sockets.emit('fldViewLoaded', currentFld, fldDetails); } }); } else { console.log('Fld Length Error') } }); }); // Save company info socket.on('saveCompanyInfo', function(payload) { var companyName = payload.companyName; var registrationNo = payload.registrationNo; var landlineNo = payload.landlineNo; var mobileNo = payload.mobileNo; var emailAddress = payload.emailAddress; var companyLogo = payload.companyLogo; var jsonFile = payload.jsonFile; var sql_save_company_info = `INSERT INTO lfr (lfr_name, registration_number, landline_number, mobile_number, email_address, lfr_logo, json_file) VALUES ('${companyName}', '${registrationNo}', '${landlineNo}', '${mobileNo}', '${emailAddress}', '${companyLogo}', '${jsonFile}')`; connection.query(sql_save_company_info, function(errorLfrInfo, resultLfrInfo) { if (errorLfrInfo) { lfrSaved = false; console.log(errorLfrInfo); } if (resultLfrInfo) { lfrSaved = true; } else { lfrSaved = false; } io.sockets.emit('companyInfoSaved', lfrSaved); }); }); // Load user list for lfr socket.on('userListLoad', function(payload) { var lfrId = payload.lfrId; var load_user_list = `SELECT USI.user_id, USI.user_name, UT.user_type, USI.email_address, USI.passcord, US.status_type FROM user_site_info USI, user_types UT, user_status US WHERE USI.user_type_id = UT.user_type_id AND USI.user_status_id = US.user_status_id AND lfr_id = ${lfrId}`; connection.query(load_user_list, function(errorUserList, resultUserList) { if (errorUserList) { console.log(errorUserList); } else { userList = resultUserList; io.sockets.emit('userListLoaded', userList); } }); }); // Load organization form socket.on('loadOrganization', function() { io.sockets.emit('organizationLoaded'); }); // Load main form socket.on('loadMain', function() { io.sockets.emit('mainLoaded'); }); // Delete user socket.on('deleteUser', function(payload) { var lfrId = payload.lfrId; var userId = payload.userId; var delete_user = `UPDATE user_site_info SET user_status_id = '2' WHERE user_id = ${userId}`; var load_user_list = `SELECT USI.user_id, USI.user_name, UT.user_type, USI.email_address, USI.passcord, US.status_type FROM user_site_info USI, user_types UT, user_status US WHERE USI.user_type_id = UT.user_type_id AND USI.user_status_id = US.user_status_id AND lfr_id = ${lfrId}`; connection.query(delete_user, function(error, result) { if (error) { console.log(error); } if (result) { connection.query(load_user_list, function(errorUserList, resultUserList) { if (errorUserList) { console.log(errorUserList); } else { userDeleted = true; userList = resultUserList; io.sockets.emit('userDeleted', userDeleted, userList); } }); } else { userDeleted = false; } }); }); // Delete weigh in socket.on('deleteWeighIn', function(payload) { var weighInId = payload.weighInId; var sql_delete_weigh_in = `DELETE FROM weigh_in WHERE weigh_in_id = ${weighInId}`; var sql_delete_weigh_in_details = `DELETE FROM weigh_in_details WHERE weigh_in_id = ${weighInId}`; connection.query(sql_delete_weigh_in, function(errorDeleteWightIn, resultDeleteWightIn) { if (errorDeleteWightIn) { console.log(errorDeleteWightIn); } connection.query(sql_delete_weigh_in_details, function(errorDeleteWightInDetails, resultDeleteWightInDetails) { if (errorDeleteWightInDetails) { console.log(errorDeleteWightInDetails); } if (resultDeleteWightInDetails) { io.sockets.emit('weighInDeleted'); } else { console.log('Weigh-In Deletion Error'); } }); }); }); // Reset weigh-in list socket.on('resetWeighInList', function() { io.sockets.emit('weighInListReset'); }); // Save user site info socket.on('saveUserSiteInfo', function(payload) { var userName = payload.userName; var userTypeId = payload.userType; var emailAddress = payload.emailAddress; var passcord = crypto.createHash('sha1').update(payload.passcord).digest("hex"); var userStatusId = 1; var lfrId = payload.lfrId; var sql_user_site_info = `INSERT INTO user_site_info (user_name, user_type_id, email_address, passcord, user_status_id, lfr_id) VALUES ('${userName}','${userTypeId}', '${emailAddress}', '${(passcord)}','${userStatusId}', '${lfrId}')`; var load_user_list = `SELECT USI.user_id, USI.user_name, UT.user_type, USI.email_address, USI.passcord, US.status_type FROM user_site_info USI, user_types UT, user_status US WHERE USI.user_type_id = UT.user_type_id AND USI.user_status_id = US.user_status_id AND lfr_id = ${lfrId}`; connection.query(sql_user_site_info, function(errorUserInfo, resultUserInfo) { if (errorUserInfo) { userSiteInfoSaved = false; console.log(errorUserInfo); } if (resultUserInfo) { userSiteInfoSaved = true; connection.query(load_user_list, function(errorUserList, resultUserList) { if (errorUserList) { console.log(errorUserList); } else { userList = resultUserList; io.sockets.emit('userSiteInfoSaved', userSiteInfoSaved, userList); } }); } else { console.log('User Info Saving Error') } }); }); // Save weigh in info socket.on('saveWeighInRecord', function(payload) { var fldId = payload.fldId; var weighInId = payload.fldId; var productId = payload.productId; var containerId = payload.containerId; var amount = payload.amount; var netWeight = payload.netWeight; var areaId = payload.areaId; var userId = payload.userId; // Check if the record is the first of the weigh in id, var sql_check_weigh_in = `SELECT * FROM weigh_in WHERE weigh_in_id = '${weighInId}'`; connection.query(sql_check_weigh_in, function(errorCheck, resultCheck) { if (errorCheck) { console.log(errorCheck); } // If there is no recrod related to weigh in id, create the weigh in id if (resultCheck.length === 0) { var sql_weigh_in_header = `INSERT INTO weigh_in (weigh_in_id, fld_id, logged_user_id, created_time) VALUES('${weighInId}', '${fldId}', '${userId}', NOW())`; connection.query(sql_weigh_in_header, function(errorHeader, resultHeader) { if (errorHeader) { console.log(errorHeader); } }); } }); var sql_weigh_in_record = `INSERT INTO weigh_in_details (product_id, container_id, number_of_containers, net_weight, area_id, weigh_in_id) VALUES ('${productId}', '${containerId}','${amount}','${netWeight}','${areaId}','${weighInId}')`; var sql_load_records = `SELECT P.product_code, C.container, WD.number_of_containers, WD.net_weight, S.species_code, PS.state_code, G.grade, A.area_code FROM product P, container C, species S, processed_state PS, grade G, area A, weigh_in_details WD WHERE WD.product_id = P.product_id AND WD.container_id = C.container_id AND WD.area_id = A.area_id AND P.species_id = S.species_id AND P.state_id = PS.state_id AND P.grade_id = G.grade_id AND weigh_in_id = '${weighInId}' ORDER BY weigh_in_detail_id ASC`; connection.query(sql_weigh_in_record, function(errorRecord, resultRecord) { if (errorRecord) { console.log(errorRecord); } if (resultRecord) { connection.query(sql_load_records, function(errorList, resultList) { if (errorList) { console.log(errorList); } else { weighInList = resultList; io.sockets.emit('weighInRecordSaved', weighInList); } }); } else { console.log('Weigh In Saving Error') } }); }); // Load user types socket.on('loadUserTypes', function() { var sql_user_types = `SELECT user_type_id, user_type FROM user_types ORDER BY user_type ASC`; connection.query(sql_user_types, function(error, result) { if (error) { console.log(error); } if (result.length !== 0) { userTypes = result; io.sockets.emit('userTypesLoaded', userTypes); } else { console.log('User Type Error') } }); }); // Load weigh-in numbers socket.on('loadWeighInNumbers', function(payload) { var lfrId = payload.lfrId var sql_load_weigh_in = `SELECT W.weigh_in_id FROM weigh_in W, fld F WHERE W.weigh_in_id = F.fld_id AND F.lfr_id = ${lfrId}`; connection.query(sql_load_weigh_in, function(error, result) { if (error) { console.log(error); } if (result.length !== 0) { weighInNumbers = result; io.sockets.emit('weighInNumbersLoaded', weighInNumbers); } else { console.log('Weigh-In Error') } }); }); // Load fld, weigh-in weights socket.on('loadWeighInWeights', function(payload) { // weigh_in table weigh_in_id and fld table fld_id are same var weighInId = payload.weighInId; var sql_load_fld_weights = `SELECT S.species, S.species_code, SUM(FD.no_of_containers * C.content_weight) AS 'fld_weight' FROM fld_details FD, species S, container C WHERE S.species_id = FD.species_id AND FD.container_id = C.container_id AND FD.fld_id = '${weighInId}' GROUP BY S.species_code, 'fld_weight' ORDER BY S.species_code`; var sql_load_weigh_in_weights = `SELECT S.species, S.species_code, SUM(WD.net_weight) AS 'weigh_in_weight' FROM weigh_in_details WD, species S, product P, container C WHERE P.species_id = S.species_id AND WD.product_id = P.product_id AND WD.container_id = C.container_id AND WD.weigh_in_id = '${weighInId}' GROUP BY S.species_code, 'weigh_in_weight' ORDER BY S.species_code`; var sql_load_fld_info = `SELECT DATE_FORMAT(F.fld_created_time, "%Y-%m-%d") AS "fld_created_date", CONCAT(U.first_name, " ", U.last_name) AS "fld_created_by", COUNT(FD.fld_id) AS "fld_records" FROM fld F, user_personal_info U, fld_details FD WHERE F.fld_created_user_id = U.user_id AND F.fld_id = FD.fld_id AND F.fld_id = '${weighInId}'`; var sql_load_weigh_info = `SELECT DATE_FORMAT(created_time, "%Y-%m-%d") AS "weigh_in_created_date", CONCAT(U.first_name, " ", U.last_name) AS "weigh_in_created_by", COUNT(WD.weigh_in_id) AS "weigh_in_records" FROM weigh_in W, user_personal_info U, weigh_in_details WD WHERE W.logged_user_id = U.user_id AND W.weigh_in_id = WD.weigh_in_id AND W.weigh_in_id = '${weighInId}'`; connection.query(sql_load_fld_weights, function(errorFldWeights, resultFldWeights) { if (errorFldWeights) { console.log(errorFldWeights); } if (resultFldWeights.length !== 0) { connection.query(sql_load_weigh_in_weights, function(errorweighInWeights, resultWeighInWeights) { if (errorweighInWeights) { console.log(errorweighInWeights); } if (resultWeighInWeights.length !== 0) { connection.query(sql_load_fld_info, function(errorFldInfo, resultFldInfo) { connection.query(sql_load_weigh_info, function(errorWeighInInfo, resultWeighInInfo) { fldWeights = resultFldWeights; weighInWeights = resultWeighInWeights; fldHeaderInfo = resultFldInfo; weighInHeaderInfo = resultWeighInInfo; io.sockets.emit('weighInWeightsLoaded', fldWeights, weighInWeights, fldHeaderInfo, weighInHeaderInfo); }); }); } else { console.log('Weigh-In Weights Error') } }); } }); }); // Load weigh in combo boxes socket.on('loadWeighInComboBoxes', function(payload) { var lfr_id = payload.lfr_id; var sql_load_fld_numbers = ` SELECT fld_id, fld_number FROM fld WHERE lfr_id = '${lfr_id}' ORDER BY fld_number DESC `; var sql_load_product_codes = ` SELECT product_id, product_code FROM product ORDER BY product_code ASC `; var sql_load_containers = ` SELECT container_id, container FROM container WHERE lfr_id = ${lfr_id} ORDER BY container ASC`; var sql_load_area_codes = ` SELECT area_id, area_code FROM area ORDER BY area_code ASC `; connection.query(sql_load_fld_numbers, function(errorFld, resultFld) { if (errorFld) { console.log(errorFld); } connection.query(sql_load_product_codes, function(errorProducts, resultProducts) { if (errorProducts) { console.log(errorProducts); } connection.query(sql_load_containers, function(errorContainer, resultContainer) { if (errorContainer) { console.log(errorContainer); } connection.query(sql_load_area_codes, function(errorArea, resultArea) { if (errorArea) { console.log(errorArea); } fldNumbers = resultFld; productCodes = resultProducts; containerTypes = resultContainer; areaCodes = resultArea; io.sockets.emit('weighInComboBoxesLoaded', fldNumbers, productCodes, containerTypes, areaCodes); }); }); }); }); }); // Get fld info and weigh in records that are relavent to weighInId socket.on('loadFldWeighInInfo', function(payload) { var fldId = payload.fldId; var weighInId = payload.fldId; var sql_get_fld_count = `SELECT COUNT( * ) AS 'fld_rows' FROM fld_details WHERE fld_id = '${fldId}'`; var sql_get_fld_info = `SELECT DATE_FORMAT(F.fld_created_time, "%Y-%m-%d") AS "fld_created_date", DATE_FORMAT(F.fld_created_time, "%T") AS "fld_created_time", CONCAT(U.first_name, " ", U.last_name) AS "created_by" FROM fld F, user_personal_info U WHERE F.fld_created_user_id = U.user_id AND F.fld_id = '${fldId}'`; var sql_load_records = `SELECT P.product_code, C.container, WD.number_of_containers, WD.net_weight, S.species_code, PS.state_code, G.grade, A.area_code FROM product P, container C, species S, processed_state PS, grade G, area A, weigh_in_details WD WHERE WD.product_id = P.product_id AND WD.container_id = C.container_id AND WD.area_id = A.area_id AND P.species_id = S.species_id AND P.state_id = PS.state_id AND P.grade_id = G.grade_id AND weigh_in_id = '${weighInId}' ORDER BY weigh_in_detail_id ASC `; connection.query(sql_get_fld_count, function(errorFldCount, resultFldCount) { if (errorFldCount) { console.log(errorFldCount); } connection.query(sql_get_fld_info, function(errorFldInfo, resultFldInfo) { if (errorFldInfo) { console.log(errorFldInfo); } connection.query(sql_load_records, function(errorList, resultList) { if (errorList) { console.log(errorList); } else { fldRows = resultFldCount; fldInfo = resultFldInfo; weighInList = resultList; io.sockets.emit('fldWeighInInfoLoaded', fldRows, fldInfo, weighInList); } }); }); }); }); // Get product info socket.on('loadProductInfo', function(payload) { var productId = payload.productId; var sql_get_product_info = `SELECT P.product_code, S.species_code, PS.state_code, G.grade FROM product P, species S, processed_state PS, grade G WHERE P.species_id = S.species_id AND P.state_id = PS.state_id AND P.grade_id = G.grade_id AND P.product_id = '${productId}'`; connection.query(sql_get_product_info, function(error, result) { if (error) { console.log(error); } if (result.length !== 0) { productInfo = result; io.sockets.emit('productInfoLoaded', productInfo); } else { console.log('No Records Found') } }); }); // Load user types socket.on('checkUserName', function(payload) { var userName = payload.userName; var sql_check_user_name = `SELECT * from user_site_info WHERE user_name = '${userName}'`; connection.query(sql_check_user_name, function(error, result) { if (error) { console.log(error); } if (result.length !== 0) { validUserName = false; } else { validUserName = true; } io.sockets.emit('userNameChecked', validUserName); }); }); // socket.emit => emit events that are handled by the client socket.emit('welcome', { title: title, flds: flds, currentFld: currentFld, fldDetails: fldDetails, lfrSaved: lfrSaved, userSiteInfoSaved: userSiteInfoSaved, userList: userList, userTypes: userTypes, fldNumbers: fldNumbers, productCodes: productCodes, containerTypes: containerTypes, areaCodes: areaCodes, fldRows: fldRows, fldInfo: fldInfo, productInfo: productInfo, weighInList: weighInList, weighInNumbers: weighInNumbers, weighInWeights: weighInWeights, fldHeaderInfo: fldHeaderInfo, weighInHeaderInfo: weighInHeaderInfo, fldWeights: fldWeights, userDeleted: userDeleted }); connections.push(socket); }); console.log(`Fishery Logistics is running at port ${ server.address().port }`);
You should definitely break this into several modules. Rule of thumb is to have controller modules and models.
TypeError: Cannot read property 'emails' of null
Here is line 54, where I am getting the error: if (docr.emails) { And here is the rest of my original code: var MongoClient = require('mongodb').MongoClient; var assert = require('assert'); var ObjectId = require('mongodb').ObjectID; var config = require('./config'), xlsx = require('./xlsx'), utils = require('./utils'), _ = require('lodash'), url = config.DB_URL; var meetings = []; function findNumberOfNotesByMeeting(db, meeting, callback) { var meetingId = meeting._id.toString(), meetingName = meeting.name.displayValue, attendees = meeting.attendees; host = meeting.host; var count = 1, pending = 0, accepted = 0; console.log("==== Meeting: " + meetingName + '===='); _.each(attendees, function(item) { console.log(count++ + ': ' + item.email + ' (' + item.invitationStatus + ')'); if (item.invitationStatus == 'pending') { pending++; } else if (item.invitationStatus == 'accepted') { accepted++; } }); console.log("*** " + attendees.length + ", " + pending + "," + accepted); db.collection('users').findOne({'_id': new ObjectId(host)}, function(err, doc) { var emails = []; if (doc.emails) { doc.emails.forEach(function(e) { emails.push(e.email + (e.primary ? '(P)' : '')); }); } var email = emails.join(', '); if (utils.toSkipEmail(email)) { callback(); } else { db.collection('notes').find({ 'meetingId': meetingId }).count(function(err, count) { if (count != 0) { console.log(meetingName + ': ' + count + ',' + attendees.length + ' (' + email + ')'); meetings.push([ meetingName, count, email, attendees.length, pending, accepted ]); } callback(); }); } }); } function findMeetings(db, meeting, callback) { var host = meeting.host; db.collection('users').findOne({'_id': new ObjectId(host)}, function(err, docr) { var emails = []; if (docr.emails) { docr.emails.forEach(function(e) { emails.push(e.email + (e.primary ? '(P)' : '')); }); } var email = emails.join(', '); if (utils.toSkipEmail(email)) { callback(); } else { var cursor = db.collection('meetings').find({ 'email': {'$regex': 'abc', '$options': 'i' } }); } cursor.count(function(err, count) { console.log('count: ' + count); var cnt = 0; cursor.each(function(err, doc) { assert.equal(err, null); if (doc != null) { findNumberOfNotesByMeeting(db, doc, function() { cnt++; if (cnt >= count) { callback(); } }); } }); }); }); }; MongoClient.connect(url, function(err, db) { assert.equal(null, err); findMeetings(db, function() { var newMeetings = meetings.sort(function(m1, m2) { return m2[1] - m1[1]; }); newMeetings.splice(0, 0, [ 'Meeting Name', 'Number of Notes', 'Emails' ]); xlsx.writeXLSX(newMeetings, config.xlsxFileNameMeetings); db.close(); }); });
Try the following: function findMeetings(db, meeting, callback) { var host = meeting.host; db.collection('users').findOne({'_id': new ObjectId(host)}, function(err, docr) { var emails = []; if (!err && docr && docr.emails) { docr.emails.forEach(function(e) { emails.push(e.email + (e.primary ? '(P)' : '')); }); } ...