我在测试Google Cloud Function时遇到问题

时间:2020-05-13 18:35:20

标签: python google-cloud-platform google-cloud-functions

这是我第一次使用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

如何解决我的问题?

1 个答案:

答案 0 :(得分:0)

此处的request对象对应于Flask的Request对象,这意味着您可以尝试从请求中获取JSON,而无需尝试转储/加载request对象本身。

location = request.get_json()

此处有更多详细信息:https://flask.palletsprojects.com/en/1.1.x/api/#flask.Request.get_json