遇到内部服务器错误 - 如何在我的 Web 应用程序中排查并解决此问题?

问题描述 投票:0回答:1

App.jsx:39 删除餐厅时出错。服务器返回 500 内部服务器错误

我正在构建一个使用 Flask 后端 API 的 React 应用程序,当我尝试从前端删除时,它一直告诉我内部服务器错误,我已尝试修复该问题,但它一直持续存在。这就是我编写代码的方式在我的后台删除:

def delete(self, restaurant_id):
        restaurant = Restaurant.query.get(restaurant_id)
        if restaurant:
            db.session.delete(restaurant)
            db.session.commit()
            return '', 204
        else:
            abort(404, error='Restaurant not found')

api.add_resource(RestaurantResource, '/restaurants/<int:restaurant_id>')

这就是前端代码的样子:

const handleDeleteRestaurant = (restaurantId) => {
    // Delete the selected restaurant
    fetch(`http://127.0.0.1:5000/restaurants/${restaurantId}`, {
      method: 'DELETE',
    })
      .then(response => {
        if (response.ok) {
          // Refresh the list of restaurants after deletion
          fetch('http://127.0.0.1:5000/restaurants')
            .then(response => response.json())
            .then(data => setRestaurants(data))
            .catch(error => console.error('Error fetching restaurants after deletion:', error));
        } else {
          console.error(`Error deleting restaurant. Server returned ${response.status} ${response.statusText}`);
        }
      })
      .catch(error => console.error('Error deleting restaurant:', error));
  };
python reactjs flask-sqlalchemy http-delete
1个回答
0
投票

前端和后端代码看起来不错。只需确保以下几点即可。

1.Restaurant 模型及其数据库表定义正确,包括主键。

2.确认提供的restaurant_id与您的Restaurant模型中主键的数据类型匹配。

3.SQLAlchemy 会话管理:仔细检查应用程序中的整体 SQLAlchemy 会话管理,以确保事务没有问题。

4.在后端代码中添加额外的日志记录或打印语句,以记录删除操作期间可能发生的任何错误。

© www.soinside.com 2019 - 2024. All rights reserved.