【问题标题】:Populating Table Based on Data being sent by node根据节点发送的数据填充表
【发布时间】:2017-03-29 02:31:06
【问题描述】:

我正在尝试填充一个表格,该表格将根据从节点函数接收到的结果显示书籍表格。我在 Node 中有一个函数,它在从 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}]

但这不起作用。我也没有收到错误,我不知道出了什么问题。如果有人能启发我,那就太好了。

【问题讨论】:

  • 把这个$.each(data.booksList改成这个$.each(data, function..只要使用数据,让我知道这是否有效!

标签: jquery node.js ajax mongodb


【解决方案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>';
 });

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2017-10-30
    • 2013-05-11
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多