简体   繁体   English

未捕获的类型错误:无法读取 node.js 上未定义的属性“长度”

[英]Uncaught TypeError: Cannot read property 'length' of undefined on node.js

How's it going?怎么样了?

I have been trying to put some upvotes functionalities on this web app I have been developing but I am stuck.我一直在尝试在我一直在开发的这个 web 应用程序上添加一些投票功能,但我被卡住了。 Please help.请帮忙。 The error that it is shown on Chrome's console is:它在 Chrome 控制台上显示的错误是:

Uncaught TypeError: Cannot read property 'length' of undefined at createUpvotesSection ((index):41) at XMLHttpRequest.ajax.onreadystatechange ((index):343)未捕获的类型错误:无法在 XMLHttpRequest.ajax.onreadystatechange ((index):343) 处的 createUpvotesSection ((index):41) 处读取未定义的属性“长度”

But could it be?但可以吗? I have already stated the "createUpvotesSection" and tried to use it in Index.ejs as you can see below:我已经说明了“createUpvotesSection”并尝试在 Index.ejs 中使用它,如下所示:

Index.ejs:索引.ejs:

function showNewsfeed() {
    var ajax = new XMLHttpRequest();
    ajax.open("POST", "/getNewsfeed", true);

    ajax.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {

            var response = JSON.parse(this.responseText);

            var html = "";
            for (var a = 0; a < response.data.length; a++) {
                var data = response.data[a];

                html += '<div class="central-meta item">';
                    html += '<div class="user-post">';
                        html += '<div class="friend-info">';

                            html += '<figure>';
                                html += '<img src="' + mainURL + "/" + data.user.profileImage + '" style="width: 45px; height: 45px; object-fit: cover;">';
                            html += '</figure>';

                            html += '<div class="friend-name">';
                                html += '<ins>';
                                    if (data.type == "post")
                                    {
                                        html += '<a href="/user/' + data.user.username + '">';
                                            html += data.user.username;
                                        html += '</a>';
                                    }
                                    else
                                    {
                                        html += data.user.name;
                                    }
                                html += '</ins>';

                                var createdAt = new Date(data.createdAt);
                                var date = createdAt.getDate() + "";
                                date = date.padStart(2, "0") + " de " + months[createdAt.getMonth()] + ", " + createdAt.getFullYear();

                                html += '<span>Publicado em: ' + date + '</span>';
                            html += '</div>';

                            html += '<div class="post-meta">';

                                html += '<div class="description">';
                                    html += '<p>';
                                        html += data.caption;
                                    html += '</p>';
                                html += '</div>';

                                if (data.image != "") {
                                    html += '<img src="' + mainURL + "/" + data.image + '">';
                                }

                                if (data.video != "") {
                                    html += '<video style="height: 359px; width: 100%;" controls src="' + mainURL + "/" + data.video + '"></video>';
                                }
                            
                                html += createUpvotesSection(data);
                            html += '</div>';
                        html += '</div>'
                    html += '</div>';
                html += '</div>';
            }
            document.getElementById("newsfeed").innerHTML = html;
        }
    };

Header.ejs (shorter version): Header.ejs(较短的版本):

function createUpvotesSection(data) {

                var isUpvoted = false;
                for (var b = 0; b < data.upvoters.length; b++) {
                    var upvoter = data.upvoter[b];
                    if (upvoter._id == window.user._id) {
                        isUpvoted = true;
                        break;
                    }
                }

                var html = "";

                html += '<div class="we-video-info">';
                    html += '<ul>';

                        html += '<li>';
                            var className = "";
                            if (isUpvoted) {
                                className = 'upvote';
                            } else {
                                className = "none";
                            }

                            html += '<span class="' + className +'" onclick="toggleUpvotePost(this);" data-id="' + data._id +'">';
                                html += '<i class="ti-thumb-up"></i>';
                                html += '<ins>' + data.upvoters.length + '</ins>';
                            html += '</span>';

                        html += '</li>';

                        html += '<li>';
                            html += '<span class="comment" title="Comments">';
                                html += '<i class = "fa fa-comments-o"></i>';
                                html += '<ins>' + data.comments.length + '</ins>';
                            html += '</span>';
                        html += '</li>';

                        html += '<li>';
                            html += '<span class="share" onclick="sharePost(this);" data-id="' + data._id +'">';
                                html += '<i class="ti-share"></i>';
                                html += '<ins>' + data.shares.length + '</ins>';
                            html += '</span>';
                        html +=  '</li>';

                    html += '</ul>';
                html += '</div>';

                return html;
            }

According to the error that Cannot read property 'length' of undefined on node.js that means the variable that you try to get length of it is undefined variable.根据Cannot read property 'length' of undefined on node.js错误,这意味着您尝试获取其长度的变量是未定义的变量。 try to define that first before you take the length尝试先定义它,然后再计算长度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:无法读取 node.js 中未定义的属性“通道” - Uncaught TypeError: Cannot read property 'channel' of undefined in node.js TypeError:无法读取Node.js中未定义的属性“ length” - TypeError: Cannot read property 'length' of undefined in Node.js d3js:未捕获的TypeError:无法读取未定义的属性“长度” - d3js: Uncaught TypeError: Cannot read property 'length' of undefined Uncaught TypeError:无法读取d3.js中未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined in d3.js Node.js writeFile [未捕获的类型错误:无法读取未定义的属性“writeFile”] - Node.js writeFile [Uncaught TypeError: Cannot read property 'writeFile' of undefined] 未捕获的TypeError:无法读取null Node.js的属性&#39;getContext&#39; - Uncaught TypeError: Cannot read property 'getContext' of null Node.js 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM