Monday, March 14, 2022

[SOLVED] How can I fix an issue on a Python Flask app on ec2 only where data from a table in database won't display?

Issue

On the dashboard page of my app, for some reason, it is unable to obtain any data from the "cars" table in the database. I was able to narrow this issue down to only existing on the dashboard page, of the app on the ec2. If I run on a local server it works fine. If I try to go to the route /show/<car_id> it retrieves the information about a specific vehicle fine.

@classmethod
    def getAllCars(cls):
        query = "SELECT * FROM cars AS c LEFT JOIN users AS u ON u.id = c.users_id;"
        results = connectToMySQL(cls.db).query_db(query)
        all_cars = []
        if not results:
            return all_cars
        for car in results:
            new_car = cls(car)
            userData = { 
                'id': car['users_id'],
                'first_name': car['first_name'],
                'last_name': car['last_name'],
                'email': car['email'],
                'password': car['password'],
                'created_at': car['u.created_at'],
                'updated_at': car['u.updated_at']
            }
            userD = user.User(userData)
            print(userD)
            new_car.user = userD
            all_cars.append(new_car)
        return all_cars
@app.route("/dashboard")
def dashboard():
    if "user_id" not in session:
        return redirect("/")
    data = {
        "id": session["user_id"]
    }
    return render_template("dashboard.html", user = User.getSingleUser(data), all_cars = Car.getAllCars())
<table class="table table-dark tabled-striped w-75">
            <thead class="border-bottom">
                <th class="w-25">Model</th>
                <th class="w-25">Year</th>
                <th class="w-25">Seller</th>
                <th class="w-25">Actions</th>
            </thead>
            <tbody>
                {% for car in all_cars %}
                <tr>
                        <td class="w-25">{{car.model}}</td>
                        <td class="w-25">{{car.year}}</td>
                        <td class="w-25">{{car.user.first_name}} {{car.user.last_name}}</td>
                        <td class="w-25">
                            <a href="/show/{{car.id}}" class="btn btn-success">View</a>
                        {% if session["user_id"] == car.users_id %}    
                            <a href="/edit/{{car.id}}" class="btn btn-primary">Edit</a>
                            <a href="/delete/{{car.id}}" class="btn btn-danger">Delete</a>
                        {% endif %}
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>

A screen shot of what I get locally vs ec2.

Running app locally

ec2


Solution

If possible, I would run the Flask app in debug mode to surface errors.

Also, I would debug the connectToMySQL and query_db methods. Are they returning the correct things? If your query actually returns falsey to indicate an error then your view will simply show an empty list of cars (because that's what the code defaults to, via all_cars = []).



Answered By - jarmod
Answer Checked By - Clifford M. (WPSolving Volunteer)