Home > Mobile >  How can I fix an issue on a Python Flask app on ec2 only where data from a table in database won
How can I fix an issue on a Python Flask app on ec2 only where data from a table in database won

Time:12-12

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 >
            <thead >
                <th >Model</th>
                <th >Year</th>
                <th >Seller</th>
                <th >Actions</th>
            </thead>
            <tbody>
                {% for car in all_cars %}
                <tr>
                        <td >{{car.model}}</td>
                        <td >{{car.year}}</td>
                        <td >{{car.user.first_name}} {{car.user.last_name}}</td>
                        <td >
                            <a href="/show/{{car.id}}" >View</a>
                        {% if session["user_id"] == car.users_id %}    
                            <a href="/edit/{{car.id}}" >Edit</a>
                            <a href="/delete/{{car.id}}" >Delete</a>
                        {% endif %}
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>

A screen shot of what I get locally vs ec2.

Running app locally

ec2

CodePudding user response:

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 = []).

  • Related