最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - List.JS - Cannot read property 'childNodes' of undefined - Stack Overflow

programmeradmin3浏览0评论

I cannot seem to figure this out. I've already tried $(document).ready and still doesn't work for me. I've also tried making a for loop specifically for these two value names to save the results to a var and pass it in that way. I've also tried putting the input with the class and id with search inside of the parent div and outside. Essentially like it in the nav bar. Using list.js & knockout.js btw. Im getting my venues using an ajax request using foursquares api.

JS:

        var options = {
        valueNames: ['name', 'category']
    };

    var userList = new List('search', options);

HTML:

<body>
<nav>
    <h1 class="formattedH1">Downtown SA,TX</h1>
    <span>
      <input type="search" placeholder="Search" class="search" id="search">
      <input type="button" class="sort searchButton" value="List"></input>
    </span>
</nav>
<div id="map" class="map"></div>
<div class="list">
    <input type="search" placeholder="Search" class="search" id="search">
    <h1>My Top 5</h1>
    <!-- Square card -->
    <div class="card" id="favViewModel" data-bind="foreach: favList">
        <div class="expand">
            <h2 class="venueName" data-bind="text:name"></h2>
        </div>
        <div class="cardSupport" data-bind="attr: {src: image()}">
        </div>
        <div class="cardSupport" data-bind="text:address">
        </div>
        <a data-bind="attr: {href: website()}">Website</a>
    </div>
    <h1>Foursquare Recommended</h1>
    <div class="card" id="recViewModel" data-bind="foreach: recommendedSpotList  ">
        <div class="expand">
            <h2 class="venueName" data-bind="text:name"></h2>
        </div>
        <div class="cardSupport" data-bind="text:location">
        </div>
        <div class="cardSupport" data-bind="text:category">
        </div>
        <div class="cardSupport" data-bind="text:rating">
        </div>
    </div>
    <script src="js/tester123.js"></script>

I cannot seem to figure this out. I've already tried $(document).ready and still doesn't work for me. I've also tried making a for loop specifically for these two value names to save the results to a var and pass it in that way. I've also tried putting the input with the class and id with search inside of the parent div and outside. Essentially like it in the nav bar. Using list.js & knockout.js btw. Im getting my venues using an ajax request using foursquares api.

JS:

        var options = {
        valueNames: ['name', 'category']
    };

    var userList = new List('search', options);

HTML:

<body>
<nav>
    <h1 class="formattedH1">Downtown SA,TX</h1>
    <span>
      <input type="search" placeholder="Search" class="search" id="search">
      <input type="button" class="sort searchButton" value="List"></input>
    </span>
</nav>
<div id="map" class="map"></div>
<div class="list">
    <input type="search" placeholder="Search" class="search" id="search">
    <h1>My Top 5</h1>
    <!-- Square card -->
    <div class="card" id="favViewModel" data-bind="foreach: favList">
        <div class="expand">
            <h2 class="venueName" data-bind="text:name"></h2>
        </div>
        <div class="cardSupport" data-bind="attr: {src: image()}">
        </div>
        <div class="cardSupport" data-bind="text:address">
        </div>
        <a data-bind="attr: {href: website()}">Website</a>
    </div>
    <h1>Foursquare Recommended</h1>
    <div class="card" id="recViewModel" data-bind="foreach: recommendedSpotList  ">
        <div class="expand">
            <h2 class="venueName" data-bind="text:name"></h2>
        </div>
        <div class="cardSupport" data-bind="text:location">
        </div>
        <div class="cardSupport" data-bind="text:category">
        </div>
        <div class="cardSupport" data-bind="text:rating">
        </div>
    </div>
    <script src="js/tester123.js"></script>

Share Improve this question asked Sep 11, 2015 at 22:17 alphapilgrimalphapilgrim 3,9759 gold badges32 silver badges62 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 16

I fixed the same problem with some comments on the github page of the project, just make sure to have the same names as the examples and it will work, everything must be in a <div> and the ul must have the class list

Like this

<div id="hacker-list">
   <ul class="list"></ul>
</div>

Link: https://github.com/javve/list.js/issues/9

I found my answer browsing other similar projects, so simple now. Thought it might help someone in case they ran across this. It was that since I was making an ajax call I had to to place the call to ko.applybindings inside the ajax request. The binding was out of scope, if you think about it make's sense especially if your request fails. Why even attempt to still bind the values of the request. HTML as above, and for JS ajax request please see below:

JS:

$.ajax({
    url: 'https://api.foursquare.com/v2/venues/explore',
    dataType: 'json',
    data: 'data',
    async: true,
    success: function(data) {

        venues = data['response']['groups'][0]['items'];

        //This is where I had to place the binding to get it to render properly.
        ko.applyBindings(new recommendedViewModel(), document.getElementById('recViewModel'));

    },
    error: function() {
        alert("An error occurred.");
    }
});

"Hello World" example of this error.

Where?

https://listjs.com/api/#listClass

listClass String, default: "list" What is the class of the list-container.

Case one (Missing list class):

/* error example */
var options = {
  valueNames: [ 'name', 'born' ],
};

var userList = new List('users', options);
<!-- error example -->
<div id="users">
  <input class="search" placeholder="Search" />
  <button class="sort" data-sort="name">
    Sort by name
  </button>

  <ul data-missing-list-class class="">
    <li>
      <h3 class="name">John</h3>
      <p class="born">1986</p>
    </li>
  </ul>

</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>

Case two (custom listClass missing):

I declare listClass to be no_such_class. The code trying to read childNodes of undefined.

/* error example */
var options = {
  valueNames: [ 'name', 'born' ],
  listClass: "no_such_class" /* the error */
};

var userList = new List('users', options);
<!-- error example -->
<div id="users">
  <input class="search" placeholder="Search" />
  <button class="sort" data-sort="name">
    Sort by name
  </button>

  <ul class="list">
    <li>
      <h3 class="name">No such class</h3>
      <p class="born">1986</p>
    </li>
  </ul>

</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>

**the error will not throw under stackoverflow snippet

Extra

Remember to not confuse between:

id or element

Id the element in which the list area should be initialized. OR the actual element itself.

new List(id/element, options, values);

VS: listClass What is the class of the list-container?

发布评论

评论列表(0)

  1. 暂无评论