在滚动时呈现更多的容器 ejs 不起作用

huangapple go评论69阅读模式
英文:

Render more containers on scroll ejs not working

问题

I have this frontend code

  <div class="bounties">
    <% projects.forEach(function(project, index) { %>
  
    <section class="box" style="margin: 1rem;">
      <a href="/project/<%=project.UniqueID%>">
      <article class="media" style="background: white;">
        <div class="media-content">
          <div class="content">
            <p>
              <span style="font-size: 2rem; color: #2d2d2d;margin-left: -2px;">Earn <strong
                  style="color: #5cb85c">$<%=project.DollarAmount.toLocaleString()%>!</strong></span>
              <br>
              <span><small><i class="fa-solid fa-clock" style="margin-right: 0.2rem;"></i> Listed <%= getTimePassed(project.ListedDate) %> </small> </span>
                <% if(project.Status == 'open') { %>
                  <div class="status regular tag" style=""><p style="color:black">Open</p></div>
                <% } else if(project.Status == 'claimed') { %>  
                  <div class="status claimed tag">Claimed</div>
                <% } else if(project.Status == 'closed') { %>  
                  <div class="status closed tag">Closed</div>
                <% } else if(project.Status == 'cancelled') { %>  
                  <div class="status closed tag">Cancelled</div>
                <% } else if(project.Status == 'completed') { %>  
                  <div class="status claimed tag">Completed</div> 
                <% } %>
  
<style>
                        .status.regular {
      font-size: 12px;
      height: fit-content;
      padding: 6px 10px;
      border-radius: 16px;
      background-color: lightblue;
      font-weight: 400;
      opacity: 1;
      font-weight: 400;
      color:black

    }
    .tags {
      display: flex;
      gap: 8px;
    }

    .tag {
      
      border-radius: 16px;
      background-color: #ffc6c2;
      font-size: 12px;
      color: #690D0D;
      background-color: #690D0D;
      border: 1px solid transparent;
      transition: .4s border-color;
      cursor: pointer;
    }

    .tag:hover {
      border-color: #bb7676;
    }


    .tag,
    .status,
    .edit,
    .delete {
      padding: 8px 12px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .status.closed {
      font-size: 12px;
      height: fit-content;
      padding: 6px 10px;
      background-color: #FFCCCB;
      border-radius: 16px;
    }
    .status.claimed {
      font-size: 12px;
      height: fit-content;
      padding: 6px 10px;
      background-color: lightgreen;
      border-radius: 16px;
    }

                  </style>
               
              <h3>              <%=project.Title%>
              </h3>
            </a>
             <p> <%=project.Description%>            </p>

      <a href="/project/<%=project.UniqueID%>">

          </div>
          <% project.Tags.forEach(function(tag) { %>
            <div class="tag"><%= tag %></div>
          <% }); %>



          <nav class="level is-mobile">
            <div class="level

<details>
<summary>英文:</summary>

I have this frontend code 
```html
  &lt;div class=&quot;bounties&quot;&gt;
    &lt;% projects.forEach(function(project, index) { %&gt;
  
    &lt;section class=&quot;box&quot; style=&quot;margin: 1rem;&quot;&gt;
      &lt;a href=&quot;/project/&lt;%=project.UniqueID%&gt;&quot;&gt;
      &lt;article class=&quot;media&quot; style=&quot;background: white;&quot;&gt;
        &lt;div class=&quot;media-content&quot;&gt;
          &lt;div class=&quot;content&quot;&gt;
            &lt;p&gt;
              &lt;span style=&quot;font-size: 2rem; color: #2d2d2d;margin-left: -2px;&quot;&gt;Earn &lt;strong
                  style=&quot;color: #5cb85c&quot;&gt;$&lt;%=project.DollarAmount.toLocaleString()%&gt;!&lt;/strong&gt;&lt;/span&gt;
              &lt;br&gt;
              &lt;span&gt;&lt;small&gt;&lt;i class=&quot;fa-solid fa-clock&quot; style=&quot;margin-right: 0.2rem;&quot;&gt;&lt;/i&gt; Listed &lt;%= getTimePassed(project.ListedDate) %&gt; &lt;/small&gt; &lt;/span&gt;
                &lt;% if(project.Status == &#39;open&#39;) { %&gt;
                  &lt;div class=&quot;status regular tag&quot; style=&quot;&quot;&gt;&lt;p style=&quot;color:black&quot;&gt;Open&lt;/p&gt;&lt;/div&gt;
                &lt;% } else if(project.Status == &#39;claimed&#39;) { %&gt;  
                  &lt;div class=&quot;status claimed tag&quot;&gt;Claimed&lt;/div&gt;
                &lt;% } else if(project.Status == &#39;closed&#39;) { %&gt;  
                  &lt;div class=&quot;status closed tag&quot;&gt;Closed&lt;/div&gt;
                &lt;% } else if(project.Status == &#39;cancelled&#39;) { %&gt;  
                  &lt;div class=&quot;status closed tag&quot;&gt;Cancelled&lt;/div&gt;
                &lt;% } else if(project.Status == &#39;completed&#39;) { %&gt;  
                  &lt;div class=&quot;status claimed tag&quot;&gt;Completed&lt;/div&gt; 
                &lt;% } %&gt;
  
&lt;style&gt;
                        .status.regular {
      font-size: 12px;
      height: fit-content;
      padding: 6px 10px;
      border-radius: 16px;
      background-color: lightblue;
      font-weight: 400;
      opacity: 1;
      font-weight: 400;
      color:black

    }
    .tags {
      display: flex;
      gap: 8px;
    }

    .tag {
      
      border-radius: 16px;
      background-color: #ffc6c2;
      font-size: 12px;
      color: #690D0D;
      background-color: #690D0D;
      border: 1px solid transparent;
      transition: .4s border-color;
      cursor: pointer;
    }

    .tag:hover {
      border-color: #bb7676;
    }


    .tag,
    .status,
    .edit,
    .delete {
      padding: 8px 12px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .status.closed {
      font-size: 12px;
      height: fit-content;
      padding: 6px 10px;
      background-color: #FFCCCB;
      border-radius: 16px;
    }
    .status.claimed {
      font-size: 12px;
      height: fit-content;
      padding: 6px 10px;
      background-color: lightgreen;
      border-radius: 16px;
    }

                  &lt;/style&gt;
               
              &lt;h3&gt;              &lt;%=project.Title%&gt;
              &lt;/h3&gt;
            &lt;/a&gt;
             &lt;p&gt; &lt;%=project.Description%&gt;            &lt;/p&gt;

      &lt;a href=&quot;/project/&lt;%=project.UniqueID%&gt;&quot;&gt;

          &lt;/div&gt;
          &lt;% project.Tags.forEach(function(tag) { %&gt;
            &lt;div class=&quot;tag&quot;&gt;&lt;%= tag %&gt;&lt;/div&gt;
          &lt;% }); %&gt;



          &lt;nav class=&quot;level is-mobile&quot;&gt;
            &lt;div class=&quot;level-left&quot;&gt;&lt;small&gt;
               

                  
                  &lt;button onclick=&quot;loadMoreProjects()&quot;
                  style=&quot;border-radius: 5px;&quot;
                  class=&quot;button bot_right is-small bountybutton has-text-black&quot;&gt;View More&lt;/button&gt;
              &lt;/small&gt;&lt;/div&gt;
          &lt;/nav&gt;
        &lt;/div&gt;
      &lt;/article&gt;
    &lt;/a&gt;
    &lt;/section&gt;




    &lt;% }); %&gt; 
  
  &lt;/div&gt;
&lt;/body&gt;

  
&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt; &lt;!-- Include jQuery library --&gt;


&lt;div class=&quot;bounties&quot;&gt;
  &lt;% projects.forEach(function(project, index) { %&gt;

  &lt;section class=&quot;box&quot; style=&quot;margin: 1rem;&quot;&gt;
    &lt;a href=&quot;/project/&lt;%=project.UniqueID%&gt;&quot;&gt;
    &lt;article class=&quot;media&quot; style=&quot;background: white;&quot;&gt;
      &lt;div class=&quot;media-content&quot;&gt;
        &lt;div class=&quot;content&quot;&gt;
          &lt;p&gt;
            &lt;span style=&quot;font-size: 2rem; color: #2d2d2d;margin-left: -2px;&quot;&gt;Earn &lt;strong
                style=&quot;color: #5cb85c&quot;&gt;$&lt;%=project.DollarAmount.toLocaleString()%&gt;!&lt;/strong&gt;&lt;/span&gt;
            &lt;br&gt;
            &lt;span&gt;&lt;small&gt;&lt;i class=&quot;fa-solid fa-clock&quot; style=&quot;margin-right: 0.2rem;&quot;&gt;&lt;/i&gt; Listed &lt;%= getTimePassed(project.ListedDate) %&gt; &lt;/small&gt; &lt;/span&gt;
              &lt;% if(project.Status == &#39;open&#39;) { %&gt;
                &lt;div class=&quot;status regular tag&quot; style=&quot;&quot;&gt;&lt;p style=&quot;color:black&quot;&gt;Open&lt;/p&gt;&lt;/div&gt;
              &lt;% } else if(project.Status == &#39;claimed&#39;) { %&gt;  
                &lt;div class=&quot;status claimed tag&quot;&gt;Claimed&lt;/div&gt;
              &lt;% } else if(project.Status == &#39;closed&#39;) { %&gt;  
                &lt;div class=&quot;status closed tag&quot;&gt;Closed&lt;/div&gt;
              &lt;% } else if(project.Status == &#39;cancelled&#39;) { %&gt;  
                &lt;div class=&quot;status closed tag&quot;&gt;Cancelled&lt;/div&gt;
              &lt;% } else if(project.Status == &#39;completed&#39;) { %&gt;  
                &lt;div class=&quot;status claimed tag&quot;&gt;Completed&lt;/div&gt; 
              &lt;% } %&gt;

&lt;style&gt;
                      .status.regular {
    font-size: 12px;
    height: fit-content;
    padding: 6px 10px;
    border-radius: 16px;
    background-color: lightblue;
    font-weight: 400;
    opacity: 1;
    font-weight: 400;
    color:black

  }
  .tags {
    display: flex;
    gap: 8px;
  }

  .tag {
    
    border-radius: 16px;
    background-color: #ffc6c2;
    font-size: 12px;
    color: #690D0D;
    background-color: #690D0D;
    border: 1px solid transparent;
    transition: .4s border-color;
    cursor: pointer;
  }

  .tag:hover {
    border-color: #bb7676;
  }


  .tag,
  .status,
  .edit,
  .delete {
    padding: 8px 12px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .status.closed {
    font-size: 12px;
    height: fit-content;
    padding: 6px 10px;
    background-color: #FFCCCB;
    border-radius: 16px;
  }
  .status.claimed {
    font-size: 12px;
    height: fit-content;
    padding: 6px 10px;
    background-color: lightgreen;
    border-radius: 16px;
  }

                &lt;/style&gt;
             
            &lt;h3&gt;              &lt;%=project.Title%&gt;
            &lt;/h3&gt;
          &lt;/a&gt;
           &lt;p&gt; &lt;%=project.Description%&gt;            &lt;/p&gt;

    &lt;a href=&quot;/project/&lt;%=project.UniqueID%&gt;&quot;&gt;

        &lt;/div&gt;
        &lt;% project.Tags.forEach(function(tag) { %&gt;
          &lt;div class=&quot;tag&quot;&gt;&lt;%= tag %&gt;&lt;/div&gt;
        &lt;% }); %&gt;



        &lt;nav class=&quot;level is-mobile&quot;&gt;
          &lt;div class=&quot;level-left&quot;&gt;&lt;small&gt;
             

                
                &lt;button        style=&quot;border-radius: 5px;&quot;
                class=&quot;button bot_right is-small bountybutton has-text-black&quot;&gt;View More&lt;/button&gt;
            &lt;/small&gt;&lt;/div&gt;
        &lt;/nav&gt;
      &lt;/div&gt;
    &lt;/article&gt;
  &lt;/a&gt;
  &lt;/section&gt;




  &lt;% }); %&gt; 

&lt;/div&gt;
&lt;/body&gt;
&lt;script&gt;
&lt;/script&gt;

&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;

&lt;script&gt;
function getTimePassed(dateString) {
    const date1 = new Date(dateString);
    if (isNaN(date1.getTime())) {
      return &quot;Invalid date&quot;;
    }
    const now = new Date();
    const timeDiff = now.getTime() - date1.getTime(); // Time difference in milliseconds
  
    // Define time units in milliseconds
    const oneSecond = 1000;
    const oneMinute = 60 * oneSecond;
    const oneHour = 60 * oneMinute;
    const oneDay = 24 * oneHour;
    const oneWeek = 7 * oneDay;
    const oneMonth = 30 * oneDay;
    const oneYear = 365 * oneDay;
  
    // Determine the appropriate time unit and rounded value
    if (timeDiff &lt; oneMinute) {
      return Math.round(timeDiff / oneSecond) + &quot; seconds ago&quot;;
    } else if (timeDiff &lt; oneHour) {
      return Math.round(timeDiff / oneMinute) + &quot; minutes ago&quot;;
    } else if (timeDiff &lt; oneDay) {
      return Math.round(timeDiff / oneHour) + &quot; hours ago&quot;;
    } else if (timeDiff &lt; oneWeek) {
      return Math.round(timeDiff / oneDay) + &quot; days ago&quot;;
    } else if (timeDiff &lt; oneMonth) {
      return Math.round(timeDiff / oneWeek) + &quot; weeks ago&quot;;
    } else if (timeDiff &lt; oneYear) {
      return Math.round(timeDiff / oneMonth) + &quot; months ago&quot;;
    } else {
      return Math.round(timeDiff / oneYear) + &quot; years ago&quot;;
    }
  }
  
alert(getTimePassed)
  var PAGE_SIZE = 10;
  var page = 1;
  var isLoading = false;

  const projectContainer = document.querySelector(&#39;.bounties&#39;);

  const observer = new IntersectionObserver((entries) =&gt; {
    // Check if the target element is intersecting with the viewport
    if (entries[0].isIntersecting &amp;&amp; !isLoading) {
      loadMoreProjects();
    }
  });

  const lastProject = document.querySelector(&#39;.bounties section:last-child&#39;);
  if (lastProject) {
    observer.observe(lastProject);
  }

  function loadMoreProjects() {
    const nextPage = page + 1;

    // Make an AJAX request to the &quot;/loadmore&quot; endpoint with the next page number
    $.ajax({
      url: &#39;/loadmore&#39;,
      method: &#39;GET&#39;,
      data: { page: nextPage },
      success: function (response) {
        const projects = response.projects;
        const hasMoreProjects = projects.length &gt;= PAGE_SIZE;

        const fragment = document.createDocumentFragment();
        projects.forEach(function (project) {
          const projectHTML = `
          &lt;section class=&quot;box&quot; style=&quot;margin: 1rem;&quot;&gt;
            &lt;a href=&quot;/project/${project.UniqueID}&quot;&gt;
              &lt;article class=&quot;media&quot; style=&quot;background: white;&quot;&gt;
                &lt;div class=&quot;media-content&quot;&gt;
                  &lt;div class=&quot;content&quot;&gt;
                    &lt;p&gt;
                      &lt;span style=&quot;font-size: 2rem; color: #2d2d2d;margin-left: -2px;&quot;&gt;Earn &lt;strong style=&quot;color: #5cb85c&quot;&gt;$${project.DollarAmount.toLocaleString()}!&lt;/strong&gt;&lt;/span&gt;
                      &lt;br&gt;
                      &lt;span&gt;&lt;small&gt;&lt;i class=&quot;fa-solid fa-clock&quot; style=&quot;margin-right: 0.2rem;&quot;&gt;&lt;/i&gt; Listed ${getTimePassed(project.ListedDate)}&lt;/small&gt;&lt;/span&gt;
                      ${
                        project.Status === &#39;open&#39;
                          ? &#39;&lt;div class=&quot;status regular tag&quot; style=&quot;&quot;&gt;&lt;p style=&quot;color:black&quot;&gt;Open&lt;/p&gt;&lt;/div&gt;&#39;
                          : project.Status === &#39;claimed&#39;
                          ? &#39;&lt;div class=&quot;status claimed tag&quot;&gt;Claimed&lt;/div&gt;&#39;
                          : project.Status === &#39;closed&#39;
                          ? &#39;&lt;div class=&quot;status closed tag&quot;&gt;Closed&lt;/div&gt;&#39;
                          : project.Status === &#39;cancelled&#39;
                          ? &#39;&lt;div class=&quot;status closed tag&quot;&gt;Cancelled&lt;/div&gt;&#39;
                          : project.Status === &#39;completed&#39;
                          ? &#39;&lt;div class=&quot;status claimed tag&quot;&gt;Completed&lt;/div&gt;&#39;
                          : &#39;&#39;
                      }
                      &lt;h3&gt;${project.Title}&lt;/h3&gt;
                    &lt;/p&gt;
                    &lt;p&gt;${project.Description}&lt;/p&gt;
                    &lt;a href=&quot;/project/${project.UniqueID}&quot;&gt;
                      &lt;div class=&quot;tags&quot;&gt;
                        ${project.Tags.map((tag) =&gt; `&lt;div class=&quot;tag&quot;&gt;${tag}&lt;/div&gt;`).join(&#39;&#39;)}
                      &lt;/div&gt;
                      &lt;nav class=&quot;level is-mobile&quot;&gt;
                        &lt;div class=&quot;level-left&quot;&gt;
                          &lt;small&gt;
                            &lt;button style=&quot;border-radius: 5px;&quot; class=&quot;button bot_right is-small bountybutton has-text-black&quot;&gt;View More&lt;/button&gt;
                          &lt;/small&gt;
                        &lt;/div&gt;
                      &lt;/nav&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                &lt;/div&gt;
              &lt;/article&gt;
            &lt;/a&gt;
          &lt;/section&gt;
        `;
          const tempElement = document.createElement(&#39;div&#39;);
          tempElement.innerHTML = projectHTML;
          fragment.appendChild(tempElement.firstChild);
        });

        projectContainer.appendChild(fragment);

        page = nextPage;
        isLoading = false;

        if (hasMoreProjects) {
          observer.observe(document.querySelector(&#39;.bounties section:last-child&#39;));
        } else {
          observer.disconnect();
        }
      },
      error: function (xhr, status, error) {
        console.log(&#39;Error:&#39;, error);
        isLoading = false; // Reset the isLoading flag in case of an error
      },
      beforeSend: function () {
        isLoading = true;
      },
    });
  }
&lt;/script&gt;

It fetches from this route

app.get(&#39;/loadmore&#39;, async function (req, res) {
  console.log(req.query.page)

  console.log(&#39;Load More route accessed&#39;);

  const PAGE_SIZE = 10; // Make sure this matches the frontend code

  const page = req.query.page || 1;
  const skip = (page - 1) * PAGE_SIZE;

  const openProjects = await projectSchema
    .find({ Status: &#39;open&#39;, PaidFor: true })
    .sort({ ListedDate: -1 })
    .skip(skip)
    .limit(PAGE_SIZE);

  const otherProjects = await projectSchema
    .find({ Status: { $ne: &#39;open&#39; }, PaidFor: true })
    .sort({ ListedDate: -1 })
    .skip(skip)
    .limit(PAGE_SIZE);

  const projects = openProjects.concat(otherProjects);
  console.log(projects)

  res.json({ projects }); // Send the projects as a JSON object
});

Here are some example projects:

const projects = [
 {
    _id: new ObjectId(&quot;64975f944b1fb2bd347d26ff&quot;),
    Category: &#39;Code&#39;,
    Title: &#39;yo&#39;,
    Lister: &#39;burningfalco99@gmail.com&#39;,
    Description: &#39;yyoyoyooyoyoy&#39;,
    DollarAmount: 22,
    ListingFeesCollected: 5,
    Tags: [ &#39;css&#39;, &#39;html&#39;, &#39;javascript&#39; ],
    Applicants: [],
    Criteria: &#39;none&#39;,
    Details: &#39;yoyoyoyo&#39;,
    Status: &#39;open&#39;,
    DueDate: 2023-06-24T21:26:44.115Z,
    ListedDate: &#39;Sat Jun 24 2023 14:26:44 GMT-0700 (Pacific Daylight Time)&#39;,
    Discussion: [],
    UniqueID: &#39;fdsfsdfsdf&#39;,
    PaidFor: true,
    StripeSession: &#39;&#39;,
    __v: 0
  },
  {
    _id: new ObjectId(&quot;64975f944b1fb2bd347d2701&quot;),
    Category: &#39;Code&#39;,
    Title: &#39;yo&#39;,
    Lister: &#39;burningfalco99@gmail.com&#39;,
    Description: &#39;yyoyoyooyoyoy&#39;,
    DollarAmount: 22,
    ListingFeesCollected: 5,
    Tags: [ &#39;css&#39;, &#39;html&#39;, &#39;javascript&#39; ],
    Applicants: [],
    Criteria: &#39;none&#39;,
    Details: &#39;yoyoyoyo&#39;,
    Status: &#39;open&#39;,
    DueDate: 2023-06-24T21:26:44.200Z,
    ListedDate: &#39;Sat Jun 24 2023 14:26:44 GMT-0700 (Pacific Daylight Time)&#39;,
    Discussion: [],
    UniqueID: &#39;fdsfsdfsdf&#39;,
    PaidFor: true,
    StripeSession: &#39;&#39;,
    __v: 0
  },
  {
    _id: new ObjectId(&quot;64975f944b1fb2bd347d2707&quot;),
    Category: &#39;Code&#39;,
    Title: &#39;yo&#39;,
    Lister: &#39;burningfalco99@gmail.com&#39;,
    Description: &#39;yyoyoyooyoyoy&#39;,
    DollarAmount: 22,
    ListingFeesCollected: 5,
    Tags: [ &#39;css&#39;, &#39;html&#39;, &#39;javascript&#39; ],
    Applicants: [],
    Criteria: &#39;none&#39;,
    Details: &#39;yoyoyoyo&#39;,
    Status: &#39;open&#39;,
    DueDate: 2023-06-24T21:26:44.452Z,
    ListedDate: &#39;Sat Jun 24 2023 14:26:44 GMT-0700 (Pacific Daylight Time)&#39;,
    Discussion: [],
    UniqueID: &#39;fdsfsdfsdf&#39;,
    PaidFor: true,
    StripeSession: &#39;&#39;,
    __v: 0
  },
  {
    _id: new ObjectId(&quot;64975f944b1fb2bd347d2709&quot;),
    Category: &#39;Code&#39;,
    Title: &#39;yo&#39;,
    Lister: &#39;burningfalco99@gmail.com&#39;,
    Description: &#39;yyoyoyooyoyoy&#39;,
    DollarAmount: 22,
    ListingFeesCollected: 5,
    Tags: [ &#39;css&#39;, &#39;html&#39;, &#39;javascript&#39; ],
    Applicants: [],
    Criteria: &#39;none&#39;,
    Details: &#39;yoyoyoyo&#39;,
    Status: &#39;open&#39;,
    DueDate: 2023-06-24T21:26:44.852Z,
    ListedDate: &#39;Sat Jun 24 2023 14:26:44 GMT-0700 (Pacific Daylight Time)&#39;,
    Discussion: [],
    UniqueID: &#39;fdsfsdfsdf&#39;,
    PaidFor: true,
    StripeSession: &#39;&#39;,
    __v: 0
  },
  {
    _id: new ObjectId(&quot;64975f944b1fb2bd347d2705&quot;),
    Category: &#39;Code&#39;,
    Title: &#39;yo&#39;,
    Lister: &#39;burningfalco99@gmail.com&#39;,
    Description: &#39;yyoyoyooyoyoy&#39;,
    DollarAmount: 22,
    ListingFeesCollected: 5,
    Tags: [ &#39;css&#39;, &#39;html&#39;, &#39;javascript&#39; ],
    Applicants: [],
    Criteria: &#39;none&#39;,
    Details: &#39;yoyoyoyo&#39;,
    Status: &#39;open&#39;,
    DueDate: 2023-06-24T21:26:44.368Z,
    ListedDate: &#39;Sat Jun 24 2023 14:26:44 GMT-0700 (Pacific Daylight Time)&#39;,
    Discussion: [],
    UniqueID: &#39;fdsfsdfsdf&#39;,
    PaidFor: true,
    StripeSession: &#39;&#39;,
    __v: 0
  },
  {
    _id: new ObjectId(&quot;64975f944b1fb2bd347d270b&quot;),
    Category: &#39;Code&#39;,
    Title: &#39;yo&#39;,
    Lister: &#39;burningfalco99@gmail.com&#39;,
    Description: &#39;yyoyoyooyoyoy&#39;,
    DollarAmount: 22,
    ListingFeesCollected: 5,
    Tags: [ &#39;css&#39;, &#39;html&#39;, &#39;javascript&#39; ],
    Applicants: [],
    Criteria: &#39;none&#39;,
    Details: &#39;yoyoyoyo&#39;,
    Status: &#39;open&#39;,
    DueDate: 2023-06-24T21:26:44.935Z,
    ListedDate: &#39;Sat Jun 24 2023 14:26:44 GMT-0700 (Pacific Daylight Time)&#39;,
    Discussion: [],
    UniqueID: &#39;fdsfsdfsdf&#39;,
    PaidFor: true,
    StripeSession: &#39;&#39;,
    __v: 0
  },
  {
    _id: new ObjectId(&quot;64975f944b1fb2bd347d2703&quot;),
    Category: &#39;Code&#39;,
    Title: &#39;yo&#39;,
    Lister: &#39;burningfalco99@gmail.com&#39;,
    Description: &#39;yyoyoyooyoyoy&#39;,
    DollarAmount: 22,
    ListingFeesCollected: 5,
    Tags: [ &#39;css&#39;, &#39;html&#39;, &#39;javascript&#39; ],
    Applicants: [],
    Criteria: &#39;none&#39;,
    Details: &#39;yoyoyoyo&#39;,
    Status: &#39;open&#39;,
    DueDate: 2023-06-24T21:26:44.284Z,
    ListedDate: &#39;Sat Jun 24 2023 14:26:44 GMT-0700 (Pacific Daylight Time)&#39;,
    Discussion: [],
    UniqueID: &#39;fdsfsdfsdf&#39;,
    PaidFor: true,
    StripeSession: &#39;&#39;,
    __v: 0
  },
  {
    _id: new ObjectId(&quot;64975f934b1fb2bd347d26fb&quot;),
    Category: &#39;Code&#39;,
    Title: &#39;yo&#39;,
    Lister: &#39;burningfalco99@gmail.com&#39;,
    Description: &#39;yyoyoyooyoyoy&#39;,
    DollarAmount: 22,
    ListingFeesCollected: 5,
    Tags: [ &#39;css&#39;, &#39;html&#39;, &#39;javascript&#39; ],
    Applicants: [],
    Criteria: &#39;none&#39;,
    Details: &#39;yoyoyoyo&#39;,
    Status: &#39;open&#39;,
    DueDate: 2023-06-24T21:26:43.948Z,
    ListedDate: &#39;Sat Jun 24 2023 14:26:43 GMT-0700 (Pacific Daylight Time)&#39;,
    Discussion: [],
    UniqueID: &#39;fdsfsdfsdf&#39;,
    PaidFor: true,
    StripeSession: &#39;&#39;,
    __v: 0
  },
  {
    _id: new ObjectId(&quot;64975f934b1fb2bd347d26f9&quot;),
    Category: &#39;Code&#39;,
    Title: &#39;yo&#39;,
    Lister: &#39;burningfalco99@gmail.com&#39;,
    Description: &#39;yyoyoyooyoyoy&#39;,
    DollarAmount: 22,
    ListingFeesCollected: 5,
    Tags: [ &#39;css&#39;, &#39;html&#39;, &#39;javascript&#39; ],
    Applicants: [],
    Criteria: &#39;none&#39;,
    Details: &#39;yoyoyoyo&#39;,
    Status: &#39;open&#39;,
    DueDate: 2023-06-24T21:26:43.862Z,
    ListedDate: &#39;Sat Jun 24 2023 14:26:43 GMT-0700 (Pacific Daylight Time)&#39;,
    Discussion: [],
    UniqueID: &#39;fdsfsdfsdf&#39;,
    PaidFor: true,
    StripeSession: &#39;&#39;,
    __v: 0
  },
  {
    _id: new ObjectId(&quot;64975f884b1fb2bd347d26f5&quot;),
    Category: &#39;Code&#39;,
    Title: &#39;yo&#39;,
    Lister: &#39;burningfalco99@gmail.com&#39;,
    Description: &#39;yyoyoyooyoyoy&#39;,
    DollarAmount: 22,
    ListingFeesCollected: 5,
    Tags: [ &#39;css&#39;, &#39;html&#39;, &#39;javascript&#39; ],
    Applicants: [],
    Criteria: &#39;none&#39;,
    Details: &#39;yoyoyoyo&#39;,
    Status: &#39;open&#39;,
    DueDate: 2023-06-24T21:26:32.176Z,
    ListedDate: &#39;Sat Jun 24 2023 14:26:32 GMT-0700 (Pacific Daylight Time)&#39;,
    Discussion: [],
    UniqueID: &#39;fdsfsdfsdf&#39;,
    PaidFor: true,
    StripeSession: &#39;&#39;,
    __v: 0
  }

]

When I scroll to the bottom of the page, more projects are supposed to load in everytime I reach the bottom of the rendered projects.

However, if I reach the bottom of the page, it console.logs "Load More route accessed", meaning that the route has been accessed, and the code is running. This happens sometimes only. However, even with the route being accessed, the new projects are not rendered.

How do I fix this, and also ensure new projects are rendered everytime I reach the bottom of the current projects.

I have been trying to fix this for weeks, so I appreciate your time on helping me.

答案1

得分: 0

section 元素未附加,因为 tempElement.firstChild 实际上是模板文字中的换行字符,所以尝试使用 firstElementChild 代替以获取 section HTML 元素:

尝试替换

fragment.appendChild(tempElement.firstChild);

fragment.appendChild(tempElement.firstElementChild);

<br>

然而,快速滚动会导致问题:快速滚动时,可能不会加载,因为观察者是异步的,所以可能尚未捕获到最后的 section,在这种情况下请参阅:https://stackoverflow.com/questions/61951380/intersection-observer-fails-sometimes-when-i-scroll-fast

英文:

The section element isn't appended, because tempElement.firstChild is actually a newline character from template literal, so try using firstElementChild instead to get the section HTML element:

try replacing

fragment.appendChild(tempElement.firstChild);

with

fragment.appendChild(tempElement.firstElementChild);

<br>

However, fast scrolling will cause problems: when scrolling fast, it might not load, because observer is async, so it might not catch the last section yet, in that case see: https://stackoverflow.com/questions/61951380/intersection-observer-fails-sometimes-when-i-scroll-fast

huangapple
  • 本文由 发表于 2023年6月29日 05:35:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定