Why is node Sequelize givng me a circular reference error? - javascript
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 :)
Related
Array is duplicated on every refresh using ExpressJS and NodeJS
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
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...
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 */ })
node.js webtorrent collect all files by magnet link
I have not used node.js before. Have a .txt file with list of magnet links. Want to write a json file with list of all files contained in these links. var WebTorrent = require('webtorrent'); var fs = require('fs'); var client = new WebTorrent(); var array = fs.readFileSync('yop.txt').toString().split("\n"); i = 0; while (i < array.length) { //console.log(array[i]); var magnetURI = array[i]; n = 0; client.add(magnetURI, function (torrent) { torrent.files.forEach(function (file) { //console.log( file.name) jsonString = JSON.stringify({'book': file.name}); fs.appendFile("data.json", jsonString, function (err) { if (err) {console.log(err);} else { n++ } }); if (n == torrent.files.length) {i++ } }) }) } when run gives the following error Sorry for such a terrible code.
var WebTorrent = require('webtorrent') var fs = require('fs') var stream = fs.createWriteStream("2.txt"); var client = new WebTorrent() var array = fs.readFileSync('yop.txt').toString().split("\n"); i = 0; function parseMagnet (uri){ var magnetURI = uri[i] console.log(magnetURI) client.add(magnetURI, function (torrent) { torrent.files.forEach(function (file) { writeStr = (uri[i]+ '\n'+ file.name+ '\n'); stream.write(writeStr); console.log(file.name) }); console.log('Done !') console.log(i) i += 1 parseMagnet(array); client.remove(magnetURI); }) } parseMagnet(array)
aws lambda function how to use responses to perform calculation
Trying to create a lambda function that lists tagged ec2 and tagged rds and performs an action thereafter. This is the first time i have used javascript and would appreciate some help. Please see my example below var aws = require('aws-sdk'); var ec2 = new aws.EC2(); var rds = new aws.RDS(); aws.config.region = 'us-east-1'; exports.handler = function(event,context) { if (event) { console.log(event.id); } //setup params for rds call var rdsparams = { DBInstanceIdentifier: 'master', }; //setup params for ec2 call var ec2params = { Filters: [ { Name: 'tag:role', Values: [ 'app' ], } ] }; //Get ec2 instances with app tag, may need to add a condition on running so pulled it into hash ec2.describeInstances(ec2params, function(err, appdata) { if (err) { console.log(err); return; } else { var apparray={}; for(var i = 0; i < appdata.Reservations.length; i++) { var ins = appdata.Reservations[i].Instances[0]; var id = ins.InstanceId; var state = ins.State.Name; apparray[id]=state; } console.log(apparray); context.succeed(apparray); } }); rds.describeDBInstances(rdsparams, function(err, data) { if (err) { console.log(err, err.stack); return; } else { var rdsarray={}; var rdsarray=(data); console.log(rdsarray); var ins=rdsarray[0]; var name = ins.ReadReplicaDBInstanceIdentifiers[0]; rdsarray[replicant]=name; } context.succeed(rdsarray); }); //context.done(); }; I want to return my filtered (apparray) and (rdsarray) back from my functions and perform a calculation on this within the main body of the script. Any ideas on how to do this. something like var replicas = rdsarray.length for example Thanks in advance
var aws = require('aws-sdk'); var ec2 = new aws.EC2(); var rds = new aws.RDS(); aws.config.region = 'us-east-1'; exports.handler = function(event, context) { if (event) { console.log(event.id); } //setup params for rds call var rdsparams = { DBInstanceIdentifier: 'master', }; //setup params for ec2 call var ec2params = { Filters: [{ Name: 'tag:role', Values: [ 'app' ], }] }; //Get ec2 instances with app tag, may need to add a condition on running so pulled it into hash ec2.describeInstances(ec2params, function(err, appdata) { if (err) return context.done(err, null); var apparray = {}; for (var i = 0; i < appdata.Reservations.length; i++) { var ins = appdata.Reservations[i].Instances[0]; var id = ins.InstanceId; var state = ins.State.Name; apparray[id] = state; } console.log(apparray); var resultData = {}; resultData.apparray = apparray; rds.describeDBInstances(rdsparams, function(err, data) { if (err) return context.done(err, null); var rdsarray = {}; var rdsarray = (data); console.log(rdsarray); var ins = rdsarray[0]; var name = ins.ReadReplicaDBInstanceIdentifiers[0]; rdsarray[replicant] = name; resultData.rdsarray = rdsarray; context.done(null, resultData); }); }); }; and back in the code from you are calling the lambda function var lambda = new sdk.Lambda(); var params = { FunctionName: 'arn:aws:lambda:us-west-2:1541546477777:function:MyFunction', Payload: JSON.stringify(/*your params here*/) }; lambda.invoke(params, function(err, data) { if (err) { console.log('error ===', err); return ; } var lambdaData = JSON.parse(data.Payload); // do your stuff here }); Is this what you needed? It may be broken but I hope you get the idea of it