无法读取未定义的属性(读取 'split'),我想做一些标签选项

Cannot read properties of undefined (reading 'split'), and i want to do some tags options

<div class="row no-margin video-title">
    <h6><i class="fas fa-book"></i> Tags</h6>
</div>
<div class="image">
    <ul class="list-unstyled mb-0">
        <% 
            Tags = video.Tags.split(",");
            Tags.forEach (function (tag) {
            tag = tag.trim(); %>

            <li>
                <a href="/tag_search/<%= tag %>"><%= tag %></a>
            </li>

        <% }) %>
    </ul>
</div>

我收到此错误:

TypeError:
E:\livestreaming\video_streaming\views\video-page\index.ejs:374
    372|             
    373| 
 >> 374|             <%- include("side-bar") %>
    375|             
    376|         </div>
    377|     </div>

E:\livestreaming\video_streaming\views\video-page\side-bar.ejs:39
    37|     <div class="image">
    38|         <ul class="list-unstyled mb-0">
 >> 39|             <% 
    40|                 Tags = video.Tags.split(",");
    41|                 Tags.forEach (function (tag) {
    42|                 tag = tag.trim(); %>

Cannot read properties of undefined (reading 'split')
    at eval ("E:\livestreaming\video_streaming\views\video-page\side-bar.ejs":47:35)
    at side-bar (E:\livestreaming\video_streaming\node_modules\ejs\lib\ejs.js:692:17)
    at include (E:\livestreaming\video_streaming\node_modules\ejs\lib\ejs.js:690:39)
    at eval ("E:\livestreaming\video_streaming\views\video-page\index.ejs":101:17)
    at index (E:\livestreaming\video_streaming\node_modules\ejs\lib\ejs.js:692:17)
    at tryHandleCache (E:\livestreaming\video_streaming\node_modules\ejs\lib\ejs.js:272:36)
    at View.exports.renderFile [as engine] (E:\livestreaming\video_streaming\node_modules\ejs\lib\ejs.js:489:10)
    at View.render (E:\livestreaming\video_streaming\node_modules\express\lib\view.js:135:8)
    at tryRender (E:\livestreaming\video_streaming\node_modules\express\lib\application.js:640:10)
    at Function.render (E:\livestreaming\video_streaming\node_modules\express\lib\application.js:592:3)

这意味着 video.Tags 存储了一个 undefined 值。在使用 split() 方法之前,请务必检查 video.Tags 是否有值。请参阅下面的代码:

var video = undefined;

// Cannot read properties of undefined (reading 'split')
video.split(',');

检查您的字符串是否为 undefined

const video = undefined;

if (typeof video === 'string') {
  const array = video.split(',');
  // Do something ...
} else {
  console.log('video is not a string');
}

// You could also use method chaining.
const r = video?.split(','); 
console.log(r);

Elon,你需要学会阅读错误!

Cannot read properties of undefined (reading 'split')

很明显,试图从 undefined 值访问名为 split 的 属性。

现在,您所要做的就是检查您在引发错误的代码部分中使用 split 的位置。

然后你就会知道,就是Tags = video.Tags.split(",");,这里用的是split。这意味着,video 对象中的 Tags 属性 未定义。

现在,您只需要确保 Tags 始终存在于 video 对象中并且它必须是一个字符串。