簡體   English   中英

將數據從循環傳遞到前端

[英]passing data from loop to front-end

我的后端有這個功能:

   app.get('/dashboard', async(req, res) => {
  const customers = await stripe.customers.list();

  customers.data.forEach(customer => {
    console.log(customer.metadata);
  });

  res.render('dashboard.ejs', {customer: customers})
})

在我的前端:

 <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
    <thead>
      <tr>
        <th>NAME</th>
        <th>EMAIL</th>
        <th>ACTIONS</th>
      </tr>
    </thead>
    <tbody>
      <% if(customer.length){ 
        for(var i = 0;i < customer.length;i++) { %>
      <tr>
      <td><%=customer[i].NAME%></td>
      <td><%=customer[i].EMAIL%></td>
         <% }
        }else{ %>
            <% } %>
    </tbody>
  </table>

但是,我正在嘗試將METADATA傳遞給前端。 在元數據對象中,我有nameemail 所以在前端,我試過:

<%=customer[i].METADATA.NAME%>

但它什么也沒返回。 和后端一樣。 我怎樣才能解決這個問題?

在上面的評論中,通過 fetch 我的意思是術語 fetch 您需要了解如何連接到后端以接受前端的數據。 您可以使用 ajax、axios、fetch API 等...。下面我使用 ajax 來展示如何從后端獲取數據。 如果您有任何問題,請隨時發表評論?

 var data_from_backend; $.ajax({ type: 'GET', data: 'data_to_send_to_backend', url: 'server_URL', success: function(getMetadata) { $.each(JSON.parse(getMetadata), function(myMetadata) { data_from_backend = myMetadata.the_name_of_data_set_on_backend }) }, error: function(data) { console.log('Something went wrong.'); } }); console.log(data_from_backend)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM