[英]TypeError: Cannot read property 'name' of undefined in node.js
function getPaths(pathIds, subCategory, callback){
var currentCategory = JSON.parse(JSON.stringify(subCategory));
console.log(currentCategory);
console.log(currentCategory.name);
var paths = [];
var pathDirs = "";
for (i=0; i<pathIds.length; i++) {
pathDirs += pathIds[i];
paths[i] = {dir : pathDirs, name : currentCategory.name};
currentCategory = currentCategory.categories.find(function(category){
return category.id = pathIds[i];
});
}
callback(paths);
}
I am new to js and node.js. 我是js和node.js的新手。 I receive the above error and I consider it refers to the currentCategory.name property.
我收到上述错误,并且我认为它引用了currentCategory.name属性。 When I type console.log (currentCategory), I have the entire object printed including the name property.
当我输入console.log(currentCategory)时,我打印了包括名称属性在内的整个对象。 What could have I done wrong?
我做错了什么?
Here is the stack trace I got for further info: 这是我获得的堆栈跟踪以获取更多信息:
D:\repos\onlineStore\borrislava-onlinestore\Backup\Work\node_modules\mongodb\lib\utils.js:132
throw err;
^
TypeError: Cannot read property 'name' of undefined
at getPaths (D:\repos\onlineStore\borrislava-onlinestore\Backup\Work\routes.js:124:54)
at D:\repos\onlineStore\borrislava-onlinestore\Backup\Work\routes.js:24:4
at D:\repos\onlineStore\borrislava-onlinestore\Backup\Work\routes.js:112:3
at result (D:\repos\onlineStore\borrislava-onlinestore\Backup\Work\node_modules\mongodb\lib\utils.js:414:17)
at executeCallback (D:\repos\onlineStore\borrislava-onlinestore\Backup\Work\node_modules\mongodb\lib\utils.js:406:9)
at handleCallback (D:\repos\onlineStore\borrislava-onlinestore\Backup\Work\node_modules\mongodb\lib\utils.js:128:55)
at cursor.close (D:\repos\onlineStore\borrislava-onlinestore\Backup\Work\node_modules\mongodb\lib\operations\cursor_ops.js:218:62)
at handleCallback (D:\repos\onlineStore\borrislava-onlinestore\Backup\Work\node_modules\mongodb\lib\utils.js:128:55)
at completeClose (D:\repos\onlineStore\borrislava-onlinestore\Backup\Work\node_modules\mongodb\lib\cursor.js:887:14)
at _endSession (D:\repos\onlineStore\borrislava-onlinestore\Backup\Work\node_modules\mongodb\lib\cursor.js:898:37)
There are a couple issues in your loop : 您的循环中有几个问题:
for (i=0; i<pathIds.length; i++) {
pathDirs += pathIds[i];
paths[i] = {dir : pathDirs, name : currentCategory.name};
currentCategory = currentCategory.categories.find(function(category){
return category.id = pathIds[i];
});
}
1 - Array.prototype.find will return the first element matching the condition in the return clause of the provided function. 1- Array.prototype.find将返回与提供的函数的return子句中的条件匹配的第一个元素。
If none element matches the condition, undefined
is affected to the currentCategory
variable. 如果没有元素符合条件,则
undefined
会影响currentCategory
变量。 Then, when looping again, currentCategory
is undefined and you get : 然后,当再次循环时,
currentCategory
是未定义的,您将得到:
TypeError : Cannot read property 'name' of undefined
TypeError:无法读取未定义的属性“名称”
2 - There's also a typo in your return clause. 2-return子句中也有错字。 You're setting
category.id
instead of comparing it to pathIds[i]
: 您正在设置
category.id
而不是将其与pathIds[i]
进行比较:
return category.id = pathIds[i]
Are you sure you don't want to compare them instead? 您确定不想比较它们吗? Because right now, you're setting category.id and returning it in your function.
因为现在,您要设置category.id并将其返回到函数中。 So, the function you provided to your
find
will return false if pathIds[i]
is equal to 0, and true otherwise. 因此,如果
pathIds[i]
等于0,则您提供给find
的函数将返回false,否则返回true。 Is that really what you want? 那真的是你想要的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.