這是我的代碼:
https://github.com/DavidNyan10/NewProject
const fs = require('fs');
const csv = require('csv-parser')
const myString = new Promise((resolve, reject) => {
resolve('John');
});
class myClass{
constructor(filename){
this.myArray = [];
this.myArray.push(new Promise((res) => {
fs.createReadStream('filename')
.pipe(csv(['FirstName', 'LastName', 'Address', 'Town', 'Country', 'Postcode']))
.on('data', (data) => {
return(data);
})
}))
}
async myFunction() {
for(let i = 0; i < this.myArray.length; i ) {
if (myString.includes(this.myArray[i]['phrase'])){
if (this.cooled_down(i)){
this.approve(i, myString);
}
}
}
}
cooled_down(i){
dictionary = this.myArray[i]
if (!dictionary.keys().includes('approved')){
// Means we have never approved on this person!
return True;
} else{
now = datetime.now();
duration = now - datetime.fromtimestamp(dictionary['approved']);
duration_seconds = duration.total_seconds();
hours = divmod(duration_seconds, 3600)[0];
if (hours >= 24){
return True;
} else{
console.log("Couldn't approve " dictionary['FirstName'] "Cool Down time: " 24 - hours);
}
}
return False;
}
approve(i, myString){
dictionary = this.myArray[i];
try{
setTimeout(function(){
console.log(myString);
console.log(dictionary['FirstName'])
console.log(dictionary['Postcode'])
}, 60 * 60 * 3);
}catch(e){
console.log(e);
}
now = datetime.now();
this.myArray[i]['approved'] = now.timestamp();
}
}
myObj = new myClass("myCSV.csv");
myString.then(myObj.myFunction);
它一直在說
(node:3196) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'myArray' of undefined
at myFunction (D:\projects\newproject\index.js:20:30)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3196) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:3196) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
但是正如您可以清楚地看到的那樣,this.myArray在第 10 行中明確定義了。
到底是怎么回事?
我對 javascript 類和物件很陌生,所以如果我看起來很笨,我很抱歉。
另外,有什么想法可以使第 11-17 行更好嗎?我只是將 csv 放入一個陣列中,但我不知道這是否是最好的方法。
uj5u.com熱心網友回復:
你打電話給myString.then(myObj.myFunction)。這是將this關鍵字從更改myObj為其他內容。如果要保留this關鍵字的背景關系,則需要this手動系結或呼叫該函式而不是將其傳遞給then回呼。例子:
// Binding `this` to object:
myString.then(myObj.myFunction.bind(myObj));
// or
// Calling the function inside anonymous function instead
// of passing it to `then` callback:
myString.then(() => myObj.myFunction());
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/374072.html
標籤:javascript 节点.js 类型错误
上一篇:用陣列過濾貓鼬陣列?
下一篇:在函式記憶體根特定的API請求
