简体   繁体   English

根据节点发送的数据填充表

[英]Populating Table Based on Data being sent by node

I am trying to populate a table which will show a table of books based on the result it receives from node function. 我正在尝试填充一个表,该表将根据它从节点函数接收的结果显示一个书表。 I have a function in Node which sends Books after it retrieves it from the mongodb: 我在Node中有一个函数,它从mongodb中检索书后会发送Books:

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

I want to display this sent result in a table-like format. 我想以表格形式显示此发送结果。 This is how I tried to achieve this: 这就是我试图达到的目的:

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

If anyone is wondering, this is the data that /viewBooks sends back. 如果有人想知道,这就是/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}]

But this doesn't work. 但这是行不通的。 I don't get an error as well, I don't know what's wrong. 我也没有收到错误,我也不知道怎么了。 If anyone could enlighten me it would be great. 如果有人能启发我,那就太好了。

You are using your index on the wrong part of the JSON object and you are calling data.bookList which I don't believe exists. 您在JSON对象的错误部分上使用了索引,并且在调用data.bookList ,我认为它不存在。

data.bookList.ISBN[0] is going to return an undefined value. data.bookList.ISBN[0]将返回一个未定义的值。

bookList[0].ISBN is also undefined. bookList[0].ISBN也未定义。

data[0].ISBN will return 9780730324218. data[0].ISBN将返回9780730324218。

Update your code to: 将您的代码更新为:

 $.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>';
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM