这将返回未定义:(节点:3196)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“myArray”
2021-12-06
1679
这是我的代码:
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 放入数组中,但我不知道这是否是最好的方法。
1个回答
您调用了
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());
koloml
2021-12-06