ct234
ct234

Reputation: 15

Why won't my query results display in my table on Express?

I am trying to display the results of a SQL query by calling an API in express but the results are not displayed. I can see them as an array of data packets in the console but can't get them displayed.

var express = require('express');
    var router = express.Router();

    var passport = require ('passport');

    var bcrypt = require('bcrypt');
    const saltRounds = 10;
    const mysql = require ('mysql');
    const path = require('path')

    const hbs = require('hbs');
    var bodyParser = require('body-parser');
    var urlencodedParser = bodyParser.urlencoded({ extended: false });
    var { check, validationResult } = require('express-validator/check'); 

    router.use(bodyParser.json());

   // most of body code omitted

    router.get('/tableResults',(req, res) => {
        console.log(req.session.passport.user);
        const db = require('./db_connection.js');
        var id = req.session.passport.user;

        db.query('SELECT * FROM testResults where id = ?',[id],function(error, results, fields) {  // results are displayed where the user ids match
            if(error) throw error;

             // JSON.parse(JSON.
             console.log(results);

             if ( results = null ) {
                res.redirect('/home');

             }; 
             res.render('tableResults',{
                results: results
             });
            });
    });
 <html lang="en">  // the correspontiong .hbs page to be rendered with results
    <head>
        <meta charset="utf-8">
        <title>Test Results</title>

    </head>
    <body>
      <div class="container">
        <h2>Test Results</h2>
            <button class="btn btn-success" data-toggle="modal" data-target="#testResults">Add New</button>
        <table class="table table-striped" id="mytable">
          <thead>
            <tr>
              <td> Test id</td>
              <th>Grade 1s</th>
              <th>Grade 2</th>
              <th>G3</th>
              <th>Action</th>
            </tr>
          </thead>
          <tbody>
             {{#each results}}
            <tr>
              <td>{{ test_id: results }}</td>
              <td>{{ gradeOne }}</td>
              <td>{{ gradeTwo }}</td>
              <td>{{ gradeThree }}</td>
              <td>
                <a href="javascript:void(0);" class="btn btn-sm btn-info 
        edit" data-id="{{ test_id }}" data-grade-one="{{ gradeOne }}" data- 
        grade-two="{{ gradeTwo }}" data-grade-three="{{ gradeThree }}>Edit 
        </a>
                 <a href="javascript:void(0);" class="btn btn-sm btn-danger 
        delete" data-id="{{ test_id }}">Delete</a>
              </td>
            </tr>
            {{/each}}
          </tbody>
        </table>
      </div>

I don't know why I cant see the results on the page as the data is in the console. I have jQuery CDN linked in my layout.hbs page too

Upvotes: 0

Views: 76

Answers (1)

random
random

Reputation: 7891

In the code if ( results = null ) {...} you are re-assigning ('=' is an assignment operator) the results with null value. Your console.log is just before you do re-assignment.

Instead use equality operator

if ( results == null ) {
  res.redirect('/home');
}; 

Upvotes: 1

Related Questions