Array is duplicated on every refresh using ExpressJS and NodeJS - javascript

I have been trying to send the var logtraceArr = []; value to the browser using Expressjs but one first call empty array is displayed on 2nd call array is filled with first called function and on 3rd call array is appended with the same element.
I want the content to be displayed on the first.
var express = require("express");
var router = express.Router();
const fs = require("fs");
const path = require("path");
const readline = require('readline');
const directoryPath = path.resolve(__dirname,"../logtrace-filestore/edepoze-logs");
var logtraceArr = [];
router.get("/",function(req,res,next){
const fetchFileContentParsed = async (fileContentObj) => {
var logtraceObj = {
Direction : '',
FromServer: '',
ToServer:''
};
var pipearray = fileContentObj.toString().split("|");
for( i=0;i<pipearray.length;i++){
var Direction = new Array();
Direction = pipearray[5].split("::");
if (Direction.length == 2) {
logtraceObj.Direction = Direction[1];
} else {
logtraceObj.Direction = "";
}
}
logtraceArr.push(logtraceObj);
return {
logtraceArr
}
}
// Iterates all users and returns their Github info.
const getfileContent = async (files) => {
const fileContent = files.map((file) => {
const filePath = path.join(directoryPath,file);
//console.log("filePath ----->",filePath);
const readStream = fs.createReadStream(filePath);
const fileContent = readline.createInterface({
input: readStream
});
fileContent.on('line', function(line) {
const regExp = /\[Event([^]+\])/g
var matchedContent;
if ((matchedContent = line.match(regExp)) != null){
return fetchFileContentParsed(matchedContent) // Async function that fetches the user info.
.then((a) => {return a })
}
})
})
return Promise.all(fileContent) // Waiting for all the requests to get resolved.
}
function readFiles(dirname) {
//console.log(dirname);
fs.readdir(dirname, async function (err,filenames)
getfileContent(filenames).then((a) => {return a })
});
}
res.header("Access-Control-Allow-Origin", "*");
res.contentType('application/json');
//console.log(readFiles(directoryPath))
readFiles(directoryPath,logtraceArr);
res.send(logtraceArr);
});
module.exports = router;
i want the content to be loaded and print the array only once on refresh of the page

Related

Array is duplicated on every refresh using ExpressJS and NodeJS

var express = require("express");
var router = express.Router();
const fs = require("fs");
const path = require("path");
const readline = require('readline');
const directoryPath = path.resolve(__dirname,"../logtrace-filestore/edepoze-logs");
var logtraceArr = [];
router.get("/",function(req,res,next){
const fetchFileContentParsed = async (fileContentObj) => {
var logtraceObj = {
Direction : '',
FromServer: '',
ToServer:''
};
var pipearray = fileContentObj.toString().split("|");
for( i=0;i<pipearray.length;i++){
var Direction = new Array();
Direction = pipearray[5].split("::");
if (Direction.length == 2) {
logtraceObj.Direction = Direction[1];
} else {
logtraceObj.Direction = "";
}
}
logtraceArr.push(logtraceObj);
return {
logtraceArr
}
}
// Iterates all users and returns their Github info.
const getfileContent = async (files) => {
const fileContent = files.map((file) => {
const filePath = path.join(directoryPath,file);
//console.log("filePath ----->",filePath);
const readStream = fs.createReadStream(filePath);
const fileContent = readline.createInterface({
input: readStream
});
fileContent.on('line', function(line) {
const regExp = /\[Event([^]+\])/g
var matchedContent;
if ((matchedContent = line.match(regExp)) != null){
return fetchFileContentParsed(matchedContent) // Async function that fetches the user info.
.then((a) => {return a })
}
})
})
return Promise.all(fileContent) // Waiting for all the requests to get resolved.
}
function readFiles(dirname) {
//console.log(dirname);
fs.readdir(dirname, async function (err,filenames)
getfileContent(filenames).then((a) => {return a })
});
}
res.header("Access-Control-Allow-Origin", "*");
res.contentType('application/json');
//console.log(readFiles(directoryPath))
readFiles(directoryPath,logtraceArr);
res.send(logtraceArr);
});
module.exports = router;
I have been trying to send the var logtraceArr = []; value to the browser using Expressjs but one first call empty array is displayed on 2nd call array is filled with first called function and on 3rd call array is appended with the same element.
I want the content to be displayed on the first.
You define logtraceArr in the global scope, so it never will be cleared. If you want to have an empty array for each request - define logtraceArr inside the router callback (for example on the line before calling readFiles)
Promise.all() doesn't wait if you don't await it. To wait you can return result of getfileContent from readFiles and then send request after result of readFiles. Fof example:
async function readFiles(dirname) {
fs.readdir(dirname, async function (err,filenames)
await getfileContent(filenames).then((a) => {return a })
});
return
}
res.header("Access-Control-Allow-Origin", "*");
res.contentType('application/json');
readFiles(directoryPath,logtraceArr).then(() => res.send(logtraceArr));

Why is node Sequelize givng me a circular reference error?

I created a model of my database using the "sequelize-auto" package. It has worked great up until today. For some reason this morning it started giving me this error: ERROR: MCAIntranet.initModels is not a function which is accompanied by the warning (node:35036) Warning: Accessing non-existent property 'initModels' of module exports inside circular dependency
This same function call (dataqueries.getSitemap) worked fine yesterday with no changes being made to this code. The import (MCAIntranet) for the model returns an empty JSON array for some reason.
Here is all the relevant code involved.
The controller, the sequelize query file, the initModel file and the table model in order:
const express = require('express');
const router = express.Router();
const Users = require('../components/Users');
const dataQuery = require('../components/dataQueries');
router.get('/', async (req, res, next) => {
try {
let user = await Users.getUserName(req);
//get groups and sitemap data
let groups = await Users.getUserRoles(user);
// let sitemap = await dataQuery.loadFile('./data/sitemap.json', 'ascii');
let sitemap=await dataQuery.getSitemap(); //ERROR OCCURS AT THIS LINE
let ret = { 'userRoles': groups, 'siteMap': sitemap };
res.send(JSON.stringify(ret));
} catch (ex) {
console.log("ERROR: ", ex.message);
}
})
module.exports = router;
let sql = require('mssql');
let fs = require('fs');
// let { Connection } = require('../ConfigData/database');
const MCAIntranet = require('../configData/models/mcaintranet/init-models')
const DB = require('../ConfigData/Connector');
const runQuery = async (query, database) => {
try {
let pool = await Connection(database);
let results = await pool.request().query(query);
// console.log("Query results: ", results.recordsets[0])
return results.recordset;
} catch (ex) {
// console.log("dataQueries.js 17", ex);
return "False! ", ex;
} finally {
sql.close();
}
}
const bulkInsert = async (database, table) => {
let pool = await Connection(database);
let insert = await pool.request()
await insert.bulk(table, (err, result) => {
if (err) {
console.log("ERROR: " + err)
} else {
console.log("SUCCESS", result);
}
});
}
const loadFile = async (filePath, type) => {
let file = fs.readFileSync(filePath, type);
return file;
}
const writeFile = async (filePath, data) => {
return fs.writeFile(
filePath,
data,
(res) => { console.log(res) },
() => {/* Empty callback function*/ })
}
const getSitemap = async () => {
const sequelize = DB.GetDB(DB.MCAIntranet);
const propsDB = MCAIntranet.initModels(sequelize); //ERROR OCCURS AT THIS LINE
//get all values contained for user
let props = await propsDB.tblSitemap.findAll({
attributes: ['JSON']
},
{ raw: true, output: true })
let sitemap = props[0].dataValues.JSON;
return JSON.parse(sitemap);
}
const writeSitemap = async (sitemap) => {
const sequelize = DB.GetDB(DB.MCAIntranet);
const propsDB = MCAIntranet.initModels(sequelize);
propsDB.tblSitemap.update(
{
JSON: JSON.stringify(sitemap)
},
{ where: { id: 1 } }
)
}
module.exports = {
loadFile: loadFile,
writeFile: writeFile,
runQuery: runQuery,
bulkInsert: bulkInsert,
getSitemap: getSitemap,
writeSitemap: writeSitemap,
}
var DataTypes = require("sequelize").DataTypes;
var _tblAttendance = require("./tblAttendance");
var _tblCollectorCodes = require("./tblCollectorCodes");
var _tblCollectors = require("./tblCollectors");
var _tblContactsData = require("./tblContactsData");
var _tblEmployees = require("./tblEmployees");
var _tblExpectingData = require("./tblExpectingData");
var _tblIntranetMessage = require("./tblIntranetMessage");
var _tblLANSAAccess = require("./tblLANSAAccess");
var _tblLANSAHeadings = require("./tblLANSAHeadings");
var _tblLANSAReportHeadings = require("./tblLANSAReportHeadings");
var _tblLANSAReports = require("./tblLANSAReports");
var _tblMessageDisplay = require("./tblMessageDisplay");
var _tblPDFFiles = require("./tblPDFFiles");
var _tblPagesVisited = require("./tblPagesVisited");
var _tblPreAuth = require("./tblPreAuth");
var _tblPreAuthDebtorInfo = require("./tblPreAuthDebtorInfo");
var _tblPreAuthFlatFiles = require("./tblPreAuthFlatFiles");
var _tblRetailReports = require("./tblRetailReports");
var _tblRogersFiles = require("./tblRogersFiles");
var _tblRogersTemp1 = require("./tblRogersTemp1");
var _tblRogersWirelessPayfiles = require("./tblRogersWirelessPayfiles");
var _tblRoles = require("./tblRoles");
var _tblSMS = require("./tblSMS");
var _tblSMSSent = require("./tblSMSSent");
var _tblSMSSents = require("./tblSMSSents");
var _tblShawAccts = require("./tblShawAccts");
var _tblSickDays = require("./tblSickDays");
var _tblSiteLayout = require("./tblSiteLayout");
var _tblSiteLayoutDesks = require("./tblSiteLayoutDesks");
var _tblSiteLayoutSites = require("./tblSiteLayoutSites");
var _tblSitemap = require("./tblSitemap");
var _tblStatusAccts = require("./tblStatusAccts");
var _tblStatusUpdates = require("./tblStatusUpdates");
var _tblUDWin = require("./tblUDWin");
var _tblUserProperties = require("./tblUserProperties");
var _vwUserRoles = require("./vwUserRoles");
var _tblUserRoles = require("./tblUserRoles");
var _tblUsers = require("./tblUsers");
var _tblVacationMain = require("./tblVacationMain");
var _tblVacationRequests = require("./tblVacationRequests");
var _tblVacations = require("./tblVacations");
var _vwLoggedIn = require("./vwLoggedIn");
function initModels(sequelize) {
var tblAttendance = _tblAttendance(sequelize, DataTypes);
var tblCollectorCodes = _tblCollectorCodes(sequelize, DataTypes);
var tblCollectors = _tblCollectors(sequelize, DataTypes);
var tblContactsData = _tblContactsData(sequelize, DataTypes);
var tblEmployees = _tblEmployees(sequelize, DataTypes);
var tblExpectingData = _tblExpectingData(sequelize, DataTypes);
var tblIntranetMessage = _tblIntranetMessage(sequelize, DataTypes);
var tblLANSAAccess = _tblLANSAAccess(sequelize, DataTypes);
var tblLANSAHeadings = _tblLANSAHeadings(sequelize, DataTypes);
var tblLANSAReportHeadings = _tblLANSAReportHeadings(sequelize, DataTypes);
var tblLANSAReports = _tblLANSAReports(sequelize, DataTypes);
var tblMessageDisplay = _tblMessageDisplay(sequelize, DataTypes);
var tblPDFFiles = _tblPDFFiles(sequelize, DataTypes);
var tblPagesVisited = _tblPagesVisited(sequelize, DataTypes);
var tblPreAuth = _tblPreAuth(sequelize, DataTypes);
var tblPreAuthDebtorInfo = _tblPreAuthDebtorInfo(sequelize, DataTypes);
var tblPreAuthFlatFiles = _tblPreAuthFlatFiles(sequelize, DataTypes);
var tblRetailReports = _tblRetailReports(sequelize, DataTypes);
var tblRogersFiles = _tblRogersFiles(sequelize, DataTypes);
var tblRogersTemp1 = _tblRogersTemp1(sequelize, DataTypes);
var tblRogersWirelessPayfiles = _tblRogersWirelessPayfiles(sequelize, DataTypes);
var tblRoles = _tblRoles(sequelize, DataTypes);
var tblSMS = _tblSMS(sequelize, DataTypes);
var tblSMSSent = _tblSMSSent(sequelize, DataTypes);
var tblSMSSents = _tblSMSSents(sequelize, DataTypes);
var tblShawAccts = _tblShawAccts(sequelize, DataTypes);
var tblSickDays = _tblSickDays(sequelize, DataTypes);
var tblSiteLayout = _tblSiteLayout(sequelize, DataTypes);
var tblSiteLayoutDesks = _tblSiteLayoutDesks(sequelize, DataTypes);
var tblSiteLayoutSites = _tblSiteLayoutSites(sequelize, DataTypes);
var tblSitemap = _tblSitemap(sequelize, DataTypes); //ERROR OCCURS CALLING THIS TABLE
var tblStatusAccts = _tblStatusAccts(sequelize, DataTypes);
var tblStatusUpdates = _tblStatusUpdates(sequelize, DataTypes);
var tblUDWin = _tblUDWin(sequelize, DataTypes);
var tblUserProperties = _tblUserProperties(sequelize, DataTypes);
var vwUserRoles = _vwUserRoles(sequelize, DataTypes);
var tblUserRoles = _tblUserRoles(sequelize, DataTypes);
var tblUsers = _tblUsers(sequelize, DataTypes);
var tblVacationMain = _tblVacationMain(sequelize, DataTypes);
var tblVacationRequests = _tblVacationRequests(sequelize, DataTypes);
var tblVacations = _tblVacations(sequelize, DataTypes);
var vwLoggedIn = _vwLoggedIn(sequelize, DataTypes);
return {
tblAttendance,
tblCollectorCodes,
tblCollectors,
tblContactsData,
tblEmployees,
tblExpectingData,
tblIntranetMessage,
tblLANSAAccess,
tblLANSAHeadings,
tblLANSAReportHeadings,
tblLANSAReports,
tblMessageDisplay,
tblPDFFiles,
tblPagesVisited,
tblPreAuth,
tblPreAuthDebtorInfo,
tblPreAuthFlatFiles,
tblRetailReports,
tblRogersFiles,
tblRogersTemp1,
tblRogersWirelessPayfiles,
tblRoles,
tblSMS,
tblSMSSent,
tblSMSSents,
tblShawAccts,
tblSickDays,
tblSiteLayout,
tblSiteLayoutDesks,
tblSiteLayoutSites,
tblSitemap,
tblStatusAccts,
tblStatusUpdates,
tblUDWin,
tblUserProperties,
vwUserRoles,
tblUserRoles,
tblUsers,
tblVacationMain,
tblVacationRequests,
tblVacations,
vwLoggedIn,
};
}
module.exports = initModels;
module.exports.initModels = initModels;
module.exports.default = initModels;
const sequelize = require('sequelize');
module.exports = function (sequelize, DataTypes) {
return sequelize.define('tblSitemap', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
JSON: {
type: DataTypes.TEXT,
allowNull: false
}
}, {
sequelize,
tableName: 'tblSitemap',
schema: 'dbo',
timestamps: false
});
};
As the sitemap is the first data called in the application, and contains reference data for all other components (lazy loading) I am unable to test other functions.
I am sure that I am missing something small but I can't spot it.
It turns out that the issue was case sensitivity. Node is case sensitive to path names even in a Windows environment. Therefore in the second file listed, const MCAIntranet = require('../configData/models/mcaintranet/init-models') should instead have been const MCAIntranet = require('../ConfigData/models/mcaintranet/init-models')
Note the capitalization of "C" in "../ConfigData/...."
Such a small, almost unnoticeable thing causing so much grief.
Hope this helps someone out :)

AMQJS0011E Invalid state not connected. AWS IoT core

I'm trying to have some sort of real-time dashboard that can subscribe to AWS IoT core topics and maybe publish too.
I have found a couple of items online, but I can't figure it out.
This is what I currently have:
function p4(){}
p4.sign = function(key, msg) {
const hash = CryptoJS.HmacSHA256(msg, key);
return hash.toString(CryptoJS.enc.Hex);
};
p4.sha256 = function(msg) {
const hash = CryptoJS.SHA256(msg);
return hash.toString(CryptoJS.enc.Hex);
};
p4.getSignatureKey = function(key, dateStamp, regionName, serviceName) {
const kDate = CryptoJS.HmacSHA256(dateStamp, 'AWS4' + key);
const kRegion = CryptoJS.HmacSHA256(regionName, kDate);
const kService = CryptoJS.HmacSHA256(serviceName, kRegion);
const kSigning = CryptoJS.HmacSHA256('aws4_request', kService);
return kSigning;
};
function getEndpoint() {
const REGION = "eu-west-1";
const IOT_ENDPOINT = "blablablabla-ats.iot.eu-west-1.amazonaws.com";
// your AWS access key ID
const KEY_ID = "My-key";
// your AWS secret access key
const SECRET_KEY = "my-access-token";
// date & time
const dt = (new Date()).toISOString().replace(/[^0-9]/g, "");
const ymd = dt.slice(0,8);
const fdt = `${ymd}T${dt.slice(8,14)}Z`
const scope = `${ymd}/${REGION}/iotdevicegateway/aws4_request`;
const ks = encodeURIComponent(`${KEY_ID}/${scope}`);
let qs = `X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=${ks}&X-Amz-Date=${fdt}&X-Amz-SignedHeaders=ho
const req = `GET\n/mqtt\n${qs}\nhost:${IOT_ENDPOINT}\n\nhost\n${p4.sha256('')}`;
qs += '&X-Amz-Signature=' + p4.sign(
p4.getSignatureKey( SECRET_KEY, ymd, REGION, 'iotdevicegateway'),
`AWS4-HMAC-SHA256\n${fdt}\n${scope}\n${p4.sha256(req)}`
);
return `wss://${IOT_ENDPOINT}/mqtt?${qs}`;
}
// gets MQTT client
function initClient() {
const clientId = Math.random().toString(36).substring(7);
const _client = new Paho.MQTT.Client(getEndpoint(), clientId);
// publish method added to simplify messaging
_client.publish = function(topic, payload) {
let payloadText = JSON.stringify(payload);
let message = new Paho.MQTT.Message(payloadText);
message.destinationName = topic;
message.qos = 0;
_client.send(message);
}
return _client;
}
function getClient(success) {
if (!success) success = ()=> console.log("connected");
const _client = initClient();
const connectOptions = {
useSSL: true,
timeout: 3,
mqttVersion: 4,
onSuccess: success
};
_client.connect(connectOptions);
return _client;
}
let client = {};
function init() {
client = getClient();
client.onMessageArrived = processMessage;
client.onConnectionLost = function(e) {
console.log(e);
}
}
function processMessage(message) {
let info = JSON.parse(message.payloadString);
const publishData = {
retailer: retailData,
order: info.order
};
client.publish("sc/delivery", publishData);
}
$(document).ready(() => {
init();
client.subscribe("sc/orders/");
});
I keep getting the same error; AMQJS0011E Invalid state not connected., but I do see in the requests pane of chrome that there is an connection or so... What am I doing wrong here?
I don't see any of the logs anyhow...

How to create an ipfs hash from an image source?

I have the src (data) of an image, that is not saved yet and doesn't have a path yet.
I would like to know the future ipfs hash that will result from it once it is saved and sent to ipfs.
So far I have done this, but the hashes don't match.
import { saveAs } from 'file-saver';
const dagPB = require('ipld-dag-pb')
const UnixFS = require('ipfs-unixfs')
func = async () => {
let bgImage = await import(`./images/bg.png`);
let bodyImage = await import(`./images/body.png`);
let headImage = await import(`./images/head.png`);
let eyesImage = await import(`./images/eye.png`);
let mouthImage = await import(`./images/mouth.png`);
let levelImage = await import(`./images/level.png`);
src = await mergeImages([
bgImage.default,
bodyImage.default,
headImage.default,
eyesImage.default,
mouthImage.default,
levelImage.default,
]);
image.src = src;
saveAs(image.src, `photo.png`);
const fileBuffer = Buffer.from(image.src)
const file = new UnixFS('file', fileBuffer)
dagPB.DAGNode.create(file.marshal(), (err, node) => {
if(err) return console.error(err)
console.log(node._cid.toBaseEncodedString())
})
}
What is missing or wrong ?
And here is what I did.
const Hash = require('ipfs-only-hash')
func = async () => {
let bgImage = await import(`./images/bg${ex}.png`);
let bodyImage = await import(`./images/body${ex}.png`);
let headImage = await import(`./images/head${ex}.png`);
let eyesImage = await import(`./images/eye${ex}.png`);
let mouthImage = await import(`./images/mouth${ex}.png`);
let levelImage = await import(`./images/level${ex}.png`);
src = await mergeImages([
bgImage.default,
bodyImage.default,
headImage.default,
eyesImage.default,
mouthImage.default,
levelImage.default,
]);
const endOfPrefix = src.indexOf(",");
const cleanStrData = src.slice(endOfPrefix+1);
const imageData = Buffer.from(cleanStrData, "base64");
const imageHash = await Hash.of(imageData);
console.log("fetch data CID: " + imageHash)
}
Remove the header of the data, to keep only the data and then hash it it with ipfs-hash-only.
The image hash is then written in the .json and the same process is used to hash the .json and know before hand the metadata's ipfs address.

Why is the module export variable empty?

I'm new to nodejs.
Here is my .js file. I'm trying to expose audioData variable to other functions. audioData variable value is being empty outside the function. I see the value when I print inside the function. What could be wrong?
'use strict';
var asyncrequest = require('request');
var xml2js = require('xml2js');
var parseString = xml2js.parseString;
var audioData = [];
asyncrequest("http://example.com/feed", function(error, responsemeta, body) {
parseString(body, function(err, result){
var stories = result['rss']['channel'][0]['item'];
console.log("Total stories: " + stories.length);
stories.forEach(function(entry) {
var singleObj = {}
singleObj['title'] = entry['title'][0];
singleObj['value'] = entry['enclosure'][0].$.url;
audioData.push(singleObj);
});
});
console.dir(audioData);
});
module.exports = audioData;
console.log("Program ended");
You'll have to return a promise for the audioData, not the audioData itself! You can learn more about promises elsewhere. Happily there's a promisified version of request, request-promise, that you can use like so:
'use strict';
var rp = require('request-promise');
var xml2js = require('xml2js');
var parseString = xml2js.parseString;
var audioData = [];
var promiseForAudioData = rp('http://example.com/feed')
.then(body => {
parseString(body, function(err, result){
var stories = result['rss']['channel'][0]['item'];
console.log("Total stories: " + stories.length);
stories.forEach(function(entry) {
var singleObj = {}
singleObj['title'] = entry['title'][0];
singleObj['value'] = entry['enclosure'][0].$.url;
audioData.push(singleObj);
});
});
return audioData;
})
.catch(console.error.bind(console));
module.exports = promiseForAudioData;
console.log("Program ended");
If you don't want to use promises, you can either export inside the callback or export the request method itself.
asyncrequest("http://example.com/feed", function(error, responsemeta, body) {
parseString(body, function(err, result){
var stories = result['rss']['channel'][0]['item'];
console.log("Total stories: " + stories.length);
stories.forEach(function(entry) {
var singleObj = {}
singleObj['title'] = entry['title'][0];
singleObj['value'] = entry['enclosure'][0].$.url; audioData.push(singleObj);
});
module.exports = audioData;
});
});
// Or
exports.get = function (callback) {
return asyncrequest(/* ... */, callback);
}
// Other module
require("./module").get(function (audioData) {
/* Do something */
})

Categories