这将返回未定义:(节点:3196)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“myArray”
Posted
技术标签:
【中文标题】这将返回未定义:(节点:3196)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“myArray”【英文标题】:this returns undefined: (node:3196) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'myArray' of undefined 【发布时间】:2022-01-11 10:37:20 【问题描述】:这是我的代码:
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());
【讨论】:
天哪,非常感谢!现在,代码运行没有任何错误,但问题是它没有返回 John 是谁.. @DavidNyan10 你的myString
包含Promise
。不是string
。您需要将字符串作为参数传递给myFnuction
。以上是关于这将返回未定义:(节点:3196)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“myArray”的主要内容,如果未能解决你的问题,请参考以下文章
节点 process.env.VARIABLE_NAME 返回未定义