简体   繁体   English

TypeError:无法读取node.js中未定义的属性“名称”

[英]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.

相关问题 Node.js 抛出 TypeError:无法读取未定义的属性“testModule” - Node.js throwing TypeError: Cannot read property 'testModule' of undefined TypeError:无法读取未定义的属性“ get”(Node.js) - TypeError: Cannot read property 'get' of undefined (Node.js) Node.js UnhandledPromiseRejection:TypeError:无法读取未定义的属性“ sign” - Node.js UnhandledPromiseRejection: TypeError: Cannot read property 'sign' of undefined TypeError:无法读取Node.js中未定义的属性“ length” - TypeError: Cannot read property 'length' of undefined in Node.js Node.js -&gt; TypeError:无法读取上下文中未定义的属性“then” - Node.js -> TypeError: Cannot read property 'then' of undefined at Context TypeError:无法读取未定义(Promise)(node.js)的属性“ then” - TypeError: Cannot read property 'then' of undefined (Promise) (node.js) 未捕获的类型错误:无法读取 node.js 上未定义的属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined on node.js Node.js - 类型错误:无法读取未定义的属性“exec” - Node.js - TypeError: Cannot read property 'exec' of undefined Node.js TypeError:无法读取未定义的属性“主机” - Node.js TypeError: Cannot read property 'host' of undefined 类型错误:无法读取未定义的 Node.js 的属性 - TypeError: Cannot read property of undefined Node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM