0

I am puzzled by the crash of the node app when rendering a page. Here is the code:

router.get("/",  async (req, res) => {

    console.log("BEFORE orders is filled with data");
    orders = Order.findAll({});
    console.log("orders is filled with data", orders);
    res.render('../views/main_view.ejs', {item_list: orders, partial_view: '../views/order/item_list.ejs'}, function(err, html){
        if (err) {
            console.log("render order error", err.message);
        } else {
            console.log('No render error');
            res.send(html);
        };
    });

});

Here is the console message:

BEFORE orders is filled with data
orders is filled with data Promise {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined }
No render error
Executing (default): SELECT "id", "order_no", "customer_name", "amount_rmb", "amount_crypto", "note", "order_datetime", "order_status", "payment_rcvd", "paid_at", "crypto_currency", "customer_id", "last_updated_by_id", "createdAt", "updatedAt" FROM "orders" AS "order";
[nodemon] app crashed - waiting for file changes before starting

...............

{ SequelizeDatabaseError: relation "orders" does not exist
    at Query.formatError (C:\d\code\js\mbot\node_modules\sequelize\lib\dialects\postgres\query.js:363:16)
    at query.catch.err (C:\d\code\js\mbot\node_modules\sequelize\lib\dialects\postgres\query.js:86:18)
    at tryCatcher (C:\d\code\js\mbot\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\d\code\js\mbot\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (C:\d\code\js\mbot\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (C:\d\code\js\mbot\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\d\code\js\mbot\node_modules\bluebird\js\release\promise.js:690:18)
    at _drainQueueStep (C:\d\code\js\mbot\node_modules\bluebird\js\release\async.js:138:12)
    at _drainQueue (C:\d\code\js\mbot\node_modules\bluebird\js\release\async.js:131:9)
    at Async._drainQueues (C:\d\code\js\mbot\node_modules\bluebird\js\release\async.js:147:5)
    at Immediate.Async.drainQueues (C:\d\code\js\mbot\node_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:810:20)
    at tryOnImmediate (timers.js:768:5)
    at processImmediate [as _immediateCallback] (timers.js:745:5)
  name: 'SequelizeDatabaseError',
  parent:

The app is nodejs 10.11.0-/express 4.16.3/EJS 2.6.1

On the browser, it shows the following error:

This site can’t be reached
localhost refused to connect.
Search Google for localhost 3000 api orders
ERR_CONNECTION_REFUSED

The view page is fine when feeding when orders = [];. Here is the view code:

<table class="table table-hover table-striped">
        <thead>
          <tr>
            <th scope="col">#</th>
            <th scope="col">Order Date</th>
            <th scope="col">Customer</th>
            <th scope="col">Order#</th>
            <th scope="col">Amount(RMB)</th>
            <th scope="col">Amount(USDT)</th>
            <th scope="col">Paid?</th>
          </tr>
        </thead>
        <tbody>
            <% for(var i =0; i < item_list.length; i++){%>
                    <tr>
                      <td><%= i + 1 %></td>
                      <td><%= item_list[i].order_datetime %></td>
                      <td><%= item_list[i].customer_name %></td>
                      <td><%= item_list[i].order_no %></td>
                      <td><%= item_list[i].amount_rmb %></td>
                      <td><%= item_list[i].amount_crypto %></td>
                      <td><%= item_list[i].payment_rcvd %></td>
                    </tr>

            <% } %>
        </tbody>
 </table>
 <table class="table table-striped">
      <tr>
        <th>

           <input type="button" value="Close" onclick="history.back(-1)" />
        </th>
      </tr>
 </table>     

I am new to the nodejs app and not sure what caused the error.

halfer
  • 19,824
  • 17
  • 99
  • 186
user938363
  • 9,990
  • 38
  • 137
  • 303
  • After renaming table name to `orders`, then code starts to work. Seems that `sequelize` only takes plural table name here. – user938363 Oct 20 '18 at 14:43
  • Here is an article about `sequelize` uses plural database table name by default. https://stackoverflow.com/questions/49885921/sequelize-table-name-change – user938363 Oct 20 '18 at 15:22
  • you should answer your own question instead of adding a comment, seems like you've found the solution – mihai Oct 22 '18 at 19:03

1 Answers1

0

Sequelize uses plural as default for tablename and name of users works . Here is an article about it Sequelize table name change.

user938363
  • 9,990
  • 38
  • 137
  • 303