基于节点发送的数据填充表

时间:2017-03-28 17:00:50

标签: jquery node.js ajax mongodb

我正在尝试填充一个表格,该表格将根据从节点功能收到的结果显示书籍表格。我在Node中有一个函数,它在从mongodb中检索它后发送Books:

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}]

但这不起作用。我也没有收到错误,我也不知道错误。如果有人能够启发我会很棒。

1 个答案:

答案 0 :(得分: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>';
 });