2017-03-28 59 views
0

我想填充一個表,它將根據從節點函數接收到的結果顯示書籍表。我有一個節點功能,它從MongoDB中取出後,它發送書籍:基於節點發送的數據填充表

app.get('/viewBooks', function (req, res) { 
    BookTable.find(function (err, booksList) { 
     if (err) return console.error(err); 
     res.send(booksList); 
    }); 
}); 

我想在表格中顯示,像這樣的格式發送結果。這是我想做到這一點:

<table class="table table-striped table-hover " id="booksTable"> 
    <thead> 
    <tr> 
     <th>ISBN</th> 
     <th>Book Title</th> 
     <th>Book Authors</th> 
     <th>Publisher</th> 
     <th>Description</th> 
     <th>Call Number</th> 

    </tr> 
    </thead> 
</table> 

<script> 
    $(document).ready(function() { 
     $.get('/viewBooks',function (data) { 
        var trJSON = ''; 
        $.each(data.booksList, function (i, item) { 
         trJSON += '<tr><td>' + booksList.ISBN[i] + '</td><td>' + booksList.BookTitle[i] + '</td></tr>' + booksList.BookAuthors[i] + '</td></tr>' 
          + '<tr><td>' + booksList.Publisher[i] + '<tr><td>' + booksList.Description[i] + '<tr><td>' + booksList.CallNumber[i] + '<tr><td>'; 
        }); 
        $('#booksTable').append(trJSON); 
       }); 
    }); 

</script> 

如果你想知道,這是/viewBooks發回的數據。

[{"_id":"58c7d0694172ab199c6de78e","ISBN":"9780730324218","BookTitle":"The Barefoot Investor : The Only Money Guide You'll Ever Need","BookAuthors":"Scott Pape","Publisher":"John Wiley & Sons Australia Ltd","Description":"This is the only money guide you'll ever need That's a bold claim, given there are already thousands of finance books on the shelves.","CallNumber":" 0730324214","__v":0},{"_id":"58c7d0694172ab199c6de790","ISBN":"9781447277682","BookTitle":"The Man Who Couldn't Stop","BookAuthors":"David Adam","Publisher":"Pan MacMillan","Description":"A Sunday Times Bestseller Have you ever had a strange urge to jump from a tall building, or steer your car into oncoming traffic? You are not alone.","CallNumber":"1447277686","__v":0},{"_id":"58c7d0694172ab199c6de78f","ISBN":"9781784701994","BookTitle":"When Breath Becomes Air","BookAuthors":"Paul Kalanithi","Publisher":"Vintage Publishing","Description":"This book is the New York Times Number One Bestseller. The Sunday Times Number one Bestseller.","CallNumber":"1784701998","__v":0},{"_id":"58c7d0694172ab199c6de791","ISBN":"9781447275282","BookTitle":"An Unquiet Mind:Picador Classic","BookAuthors":"Kay Redfield Jamison","Publisher":"Pan MacMillan","Description":"With an introduction by Andrew Solomon 'It stands alone in the literature of manic depression for its bravery, brilliance and beauty.' ","CallNumber":"1447275284","__v":0},{"_id":"58c7d0694172ab199c6de792","ISBN":"9780393340792","BookTitle":"Loud in the House of Myself:Memoir of a Strange Girl","BookAuthors":"Stacy Pershall","Publisher":"WW Norton & Co","Description":"Stacy Pershall grew up as an overly intelligent, depressed, deeply strange girl in Prairie Grove, Arkansas, population 1,000. From her days as a thirteen-year-old Jesus freak through her eventual diagnosis of bipolar disorder and borderline personality disorder, this spirited memoir chronicles Pershall's journey through hell and her struggle with the mental health care system.","CallNumber":"0393340791","__v":0}] 

但這不起作用。我也沒有得到一個錯誤,我不知道什麼是錯的。如果有人能夠啓發我,那會很棒。

+0

更改這個'$。每個(data.booksList'這個'$。每次(數據,function'..just使用數據,讓我知道如果這有效! – Hackerman

回答

1

您正在使用您的索引在JSON對象的錯誤部分,你打電話data.bookList,我不相信存在。

data.bookList.ISBN[0]將返回一個未定義的值。

bookList[0].ISBN也是未定義的。

data[0].ISBN將返回9780730324218.

更新您的代碼:

$.each(data, function (i, item) { 
    trJSON += '<tr><td>' + data[i].ISBN + '</td><td>' + data[i].BookTitle + '</td></tr>' + data[i].BookAuthors + '</td></tr>' 
    + '<tr><td>' + data[i].Publisher + '<tr><td>' + data[i].Description + '<tr><td>' + data[i].CallNumber + '<tr><td>'; 
});