首页 > 解决方案 > 我在测试 Google Cloud Function 时遇到问题

问题描述

这是我第一次使用 Google Cloud Functions。而且我无法运行该功能。我的代码如下所示。

import json
def location_sort(request):
    request = json.dumps(request)
    location = json.loads(request)
    reverse_location = {v: k for k, v in location.items()}



    x = location.keys()
    harf_x = (float(max(x)) + float(min(x))) / 2 

    y_right = []
    y_left = []
    sorted_location = [] 


    for i in location:
        if float(i) < harf_x:
            y_left.append(location[i])
        else: 
            y_right.append(location[i])

    y_left.sort() 
    y_right.sort(reverse=True) 

    sorted_input = y_left + y_right 


    for i in sorted_input:
        sorted_location.append([reverse_location[i], i])

    return json.dumps(sorted_location)

我把触发器

{"38:127":"127.012","37.128":"127.002"} 

并且错误显示为

Error: function terminated. Recommended action: inspect logs for termination reason. Details:
Object of type LocalProxy is not JSON serializable

我该如何解决我的问题?

标签: pythongoogle-cloud-platformgoogle-cloud-functions

解决方案


这里的request对象对应于 Flask 的Request对象,这意味着您可以从请求中获取 JSON,而不是尝试转储/加载request对象本身:

location = request.get_json()

更多细节在这里:https ://flask.palletsprojects.com/en/1.1.x/api/#flask.Request.get_json


推荐阅读