I'm not knowledgeable in JS and JS in Node RTE. But I tried writing simple functions as arrow functions in objects for later usage. One of these arrow functions (data.volatile.modularVariables.read) calls the readFile (asynchronous) function from FileSystem node native module, and passes the following into the parameters from the same object:
- file path (data.persistent.paths.srcRoot)
- encoding scheme (data.volatile.modularVariables.encoding.utf8)
- call-back function (data.volatile.modularVariables.readCB) <-issue lays here
relevant Code (the object):
var data =
{
persistent:
{
states:
{
//exempt for question clarity
},
paths:
{
srcRoot: './vortex/root.txt'
}
},
volatile:
{
//much of the data exempt for question clarity
modularVariables:
{
encoding: {utf8:'utf8',hex:'hex',base64:'base64',BIN:(data,encoding)=>{Buffer.from(data,encoding)}},
readCB: (err,data)=>{if(err){console.log(`%c${data.volatile.debug.debugStrings.errCodes.read}: Error reading from file`,'color: red'); console.log(data);}},
writeCB: (err)=>{if(err){console.log(`%c${data.volatile.debug.debugStrings.errCodes.write}: Error writing to file`, 'color:red')}},
read: (file,encoding,cb)=>{fs.readFile(file,encoding,cb)}, //cb = readCB READ file srcRoot, pass into root(rootHash,encoding)
write: (file,data,cb)=>{fs.writeFile(file,data,cb)}, //cb = writeCB
checkInit: (symbol)=>{if(typeof symbol !== undefined){return symbol}}
},
debug:
{
functions:
{
append:
{
testProg:{program:{main:'system.node.upgrade'}}
}
},
debugStrings:
{
errCodes:
{
read: 'AFTERNET ERR 000',
write: 'AFTERNET ERR 001',
}
}
}
}
};
Aggregator Code:
testing()
{
data.volatile.modularVariables.read(data.persistent.paths.srcRoot,data.volatile.modularVariables.encoding.utf8,data.volatile.modularVariables.readCB(data));
};
Terminal Error:
readCB: (err,data)=>{if(err){console.log(`%c${data.volatile.debug.debugStrings.errCodes.read}: Error reading from file`,'color: red'); console.log(data);}},
^
TypeError: Cannot read property 'volatile' of undefined
Notes:
- In aggregator code, I tried not passing "data" into callback
- I tried passing "err" but says it's undefined
- I tried not passing anything into CB
Conclusion:
Can someone point out what I'm doing wrong and why?
CodePudding user response:
I couldn't tell from the comments if you've resolved the error, but I have a few suggestions that may help.
Aggregator Code
I noticed that you are sending the callback in that code and passing in the data
object, which is assigned to the err
argument and the data
argument will be undefined:
testing();
{
data.volatile.modularVariables.read(
data.persistent.paths.srcRoot,
data.volatile.modularVariables.encoding.utf8,
data.volatile.modularVariables.readCB(data)
);
}
When passing in the callback function, you only need to specify the callback function name as below. NodeJS will call the callback with the appropriate err
and data
arguments.
data.volatile.modularVariables.readCB
I believe this should resolve your problem.
testing();
{
data.volatile.modularVariables.read(
data.persistent.paths.srcRoot,
data.volatile.modularVariables.encoding.utf8,
data.volatile.modularVariables.readCB
);
}
Variable Naming
One thing that could help others read your code and spot issues without needing to run the code is making sure you don't have variables with the same name. I believe you are attempting to reference your data
object outside of the callback function, but in the scope of the callback function, data
will be primarily referenced as the argument passed in (see scope). When I ran your code, the debugger showed me that data
was undefined before applying the fix above. After, it showed me that data
was an empty string.
For this, you can either change your data
object to be named something like myData
or cypherData
and it will not conflict with any of your other variables/parameters.
Full Solution
var cypherData = {
persistent: {
states: {
//exempt for question clarity
},
paths: {
srcRoot: "./vortex/root.txt"
}
},
volatile: {
//much of the data exempt for question clarity
modularVariables: {
encoding: {
utf8: "utf8",
hex: "hex",
base64: "base64",
BIN: (data, encoding) => {
Buffer.from(data, encoding);
}
},
readCB: (err, data) => {
console.log(data);
if (err) {
console.log(
`%c${data.volatile.debug.debugStrings.errCodes.read}: Error reading from file`,
"color: red"
);
}
},
writeCB: (err) => {
if (err) {
console.log(
`%c${data.volatile.debug.debugStrings.errCodes.write}: Error writing to file`,
"color:red"
);
}
},
read: (file, encoding, cb) => {
fs.readFile(file, encoding, cb);
}, //cb = readCB READ file srcRoot, pass into root(rootHash,encoding)
write: (file, data, cb) => {
fs.writeFile(file, data, cb);
}, //cb = writeCB
checkInit: (symbol) => {
if (typeof symbol !== undefined) {
return symbol;
}
}
},
debug: {
functions: {
append: {
testProg: { program: { main: "system.node.upgrade" } }
}
},
debugStrings: {
errCodes: {
read: "AFTERNET ERR 000",
write: "AFTERNET ERR 001"
}
}
}
}
};
cypherData.volatile.modularVariables.read(
cypherData.persistent.paths.srcRoot,
cypherData.volatile.modularVariables.encoding.utf8,
cypherData.volatile.modularVariables.readCB
);