我不了解 Node RTE 中的 JS 和 JS。但是我嘗試在物件中撰寫簡單的函式作為箭頭函式供以后使用。這些箭頭函式之一 (data.volatile.modularVariables.read) 從 FileSystem 節點本機模塊呼叫 readFile(異步)函式,并將以下內容傳遞到來自同一物件的引數中:
- 檔案路徑(data.persistent.paths.srcRoot)
- 編碼方案(data.volatile.modularVariables.encoding.utf8)
- 回呼函式 (data.volatile.modularVariables.readCB) <-issue 在這里
相關代碼(物件):
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',
}
}
}
}
};
聚合器代碼:
testing()
{
data.volatile.modularVariables.read(data.persistent.paths.srcRoot,data.volatile.modularVariables.encoding.utf8,data.volatile.modularVariables.readCB(data));
};
終端錯誤:
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
筆記:
- 在聚合器代碼中,我嘗試不將“資料”傳遞給回呼
- 我嘗試傳遞“錯誤”但說它未定義
- I tried not passing anything into CB
Conclusion:
Can someone point out what I'm doing wrong and why?
uj5u.com熱心網友回復:
我無法從評論中判斷您是否解決了錯誤,但我有一些建議可能會有所幫助。
聚合器代碼
我注意到您正在該代碼中發送回呼并傳入data分配給err引數的物件,并且該data引數將是未定義的:
testing();
{
data.volatile.modularVariables.read(
data.persistent.paths.srcRoot,
data.volatile.modularVariables.encoding.utf8,
data.volatile.modularVariables.readCB(data)
);
}
傳入回呼函式時,只需要指定如下回呼函式名即可。NodeJS 將使用適當的err和data引數呼叫回呼。
data.volatile.modularVariables.readCB
我相信這應該可以解決您的問題。
testing();
{
data.volatile.modularVariables.read(
data.persistent.paths.srcRoot,
data.volatile.modularVariables.encoding.utf8,
data.volatile.modularVariables.readCB
);
}
變數命名
可以幫助其他人閱讀您的代碼并發現問題而無需運行代碼的一件事是確保您沒有同名的變數。我相信您正在嘗試data在回呼函式之外參考您的物件,但在回呼函式的范圍內,data將主要作為傳入的引數參考(請參閱范圍)。當我運行您的代碼時,除錯器data在應用上述修復程式之前向我顯示未定義。之后,它告訴我這data是一個空字串。
為此,您可以將data物件更改為類似的名稱,myData或者cypherData它不會與您的任何其他變數/引數發生沖突。
完整解決方案
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
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/315796.html
標籤:javascript node.js object undefined arrow-functions
上一篇:如何使用字串和陣列創建物件陣列
下一篇:物件到陣列堆疊末尾的空值
