我是Goggle Cloud Function的新手,所以我不确定我的错误可能是什么,因为它随测试的不同而变化
首先,让我们看一些代码:
package my_test
import (
"encoding/json"
"errors"
"fmt"
"{source_host}/{project}/my_test/models"
"net/http"
)
// HttpMain prints the JSON encoded "UserId" field in the body
// of the request.
func HttpMain(w http.ResponseWriter, r *http.Request) {
var body models.Body
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
_, _ = fmt.Fprintf(w,"%v\n", err)
}
err := Run(w, &body)
if err != nil {
_, _ = fmt.Fprintf(w,"%v\n", err)
}
_, _ = fmt.Fprintln(w, "Done")
}
// Run the core part of the function itself.
func Run(w http.ResponseWriter, body *models.Body) error {
if body == nil {
return errors.New("body parameter is nil")
} else if body.UserId == "" {
return errors.New("body.UserId is empty")
} else {
_, err := fmt.Fprintf(w, "%s\n", body.UserId)
return err
}
}
身体模型如下:
package models
type Body struct {
UserId string `protobuf:"bytes,1,opt,name=UserId,proto3" json:"UserId,omitempty"`
}
我制作了一个简单的shell脚本来部署我的功能:
#!/usr/bin/env bash
name="my_test"
entryPoint="HttpMain"
yes Y | gcloud functions deploy ${name} --trigger-http --entry-point ${entryPoint} --region europe-west1 --runtime go113 --allow-unauthenticated
该功能已部署,一切正常。
好吧...我有3个测试,每个测试都会引发不同的错误,我完全不知道为什么:
Gcloud命令行
让我们从命令行工具本身开始。我有这个测试:
#!/usr/bin/env bash
name="my_test"
gcloud functions call ${name} --region europe-west1 --data "{\"UserId\":\"Hey yo buddy\"}"
结果:
macbook-pro-de-emixam23:my_test emixam23$ ./test.sh
error: 'Error: cannot communicate with function.'
日志:
D 2020-04-30T09:36:13.702363769Z my_test s1tyqfgxld9u Function execution started my_test s1tyqfgxld9u
A 2020-04-30T09:36:13.723Z my_test s1tyqfgxld9u 2020/04/30 09:36:13 project id is required to access Firestore my_test s1tyqfgxld9u
D 2020-04-30T09:36:13.813514210Z my_test s1tyqfgxld9u Function execution took 104 ms, finished with status: 'connection error' my_test s1tyqfgxld9u
邮递员
然后我从邮递员尝试了
https://europe-west1-{PROJECT_ID}.cloudfunctions.net/my_test
{
"data": {
"UserId":"Hey yo buddy"
}
}
请检查https://stackoverflow.com/a/58210273/6093604,以更好地了解数据字段
结果:
body.UserId is empty
Done
日志:
D 2020-04-30T09:40:35.019776155Z my_test 9orqtgampnqz Function execution started my_test 9orqtgampnqz
D 2020-04-30T09:40:35.246508146Z my_test 9orqtgampnqz Function execution took 227 ms, finished with status code: 200 my_test 9orqtgampnqz
端子
然后我使用cURL在终端上尝试,但又出现了另一个新问题...
macbook-pro-de-emixam23:~ emixam23$ curl -X POST "https://europe-west1-{PROJECT_ID}.cloudfunctions.net/my_test" -H "Content-Type:application/json" --data '{"UserId":"azerty"}'
结果:
Error: could not handle the request
日志:
D 2020-04-30T09:41:51.645842954Z my_test k856kvp2bhun Function execution started my_test k856kvp2bhun
A 2020-04-30T09:41:51.698Z my_test k856kvp2bhun 2020/04/30 09:41:51 project id is required to access Firestore my_test k856kvp2bhun
D 2020-04-30T09:41:51.712630551Z my_test k856kvp2bhun Function execution took 67 ms, finished with status: 'connection error' my_test k856kvp2bhun
那到底是怎么回事...怎么可能,跟随文档,我会遇到3种不同的错误...
感谢您的帮助。
答案 0 :(得分:0)
您可以检查Google控制台下的日志以获取更多详细信息,并在此处传递 谢谢。
答案 1 :(得分:0)
我终于明白了...
好吧,我可能没有输入“嘿,我使用Firestore”的信息。正如我首先想到的那样,我的文件/文件夹被上传并“瞧”。好吧,
必须考虑两件事:
我如何解决它并知道这一点?再次不是来自医生,而是...我模拟了一个“ ls -la”,它记录了我所有的文件/文件夹...
希望有帮助!