Firebase 将数据从curl发布到Firestore时出现问题
我是Google Firestore(也是Firebase)的初学者。我正在做一个简单的POC,我有点痛苦与FireStore互动。嗯,从Angular那里读取数据非常容易。然而,像从curl添加新数据或从FireBase Cli删除集合这样的简单操作花费了我一整天的时间,没有任何进展。为了强调我在FireStore中是多么的愚蠢,我已经安装了ElasticSearch local,所以我尝试了类似的行为/工具/功能,比如从curl/postman发布/创建字段(这不是问题的一部分,只是暴露了我的局限性),但我无法前进 这就是我从Angular成功连接的方式:Firebase 将数据从curl发布到Firestore时出现问题,firebase,curl,firebase-realtime-database,google-cloud-firestore,firebase-cli,Firebase,Curl,Firebase Realtime Database,Google Cloud Firestore,Firebase Cli,我是Google Firestore(也是Firebase)的初学者。我正在做一个简单的POC,我有点痛苦与FireStore互动。嗯,从Angular那里读取数据非常容易。然而,像从curl添加新数据或从FireBase Cli删除集合这样的简单操作花费了我一整天的时间,没有任何进展。为了强调我在FireStore中是多么的愚蠢,我已经安装了ElasticSearch local,所以我尝试了类似的行为/工具/功能,比如从curl/postman发布/创建字段(这不是问题的一部分,只是暴露了我
firebaseConfig : {
apiKey: "*** removed ***",
authDomain: "transfer-status-realtime.firebaseapp.com",
databaseURL: "https://transfer-status-realtime.firebaseio.com",
projectId: "transfer-status-realtime",
storageBucket: "transfer-status-realtime.appspot.com",
messagingSenderId: "143612370857",
appId: "1:143612370857:web:a9d0b21e3a58520ed02eb8",
measurementId: "G-WT5C346D7T"
}
我主要将我的潜能放在和上
我最近一次尝试:
c:\temp>curl -X POST -H "Content-Type: application/json" -d'{"fields": {"status": "success"}}' "https://firestore.googleapis.com/v1beta1/projects/transfer-status-realtime/databases/(default)/documents/transfers?&key=***removed***"
curl: (3) [globbing] unmatched brace in column 1
curl: (3) [globbing] unmatched close brace/bracket in column 8
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Closing quote expected in string.\n\n^",
"status": "INVALID_ARGUMENT"
}
}
所以我的主要问题是:如何从curl向FireStore添加数据
如果有问题,这里是我的数据库控制台:
我尝试过的其他可能方向错误的方法有:
curl -X PUT -d '{
"transfers": {
"id": "idfromPut",
"status": 1
}
}' 'https://transfer-status-realtime.firebaseio.com/rest/saving-data/fireblog/users.json'
curl-X POST-d'{“状态”:2}''https://transfer-status-realtime.firebaseio.com/message_list.json“
c:\temp>curl -X POST -H "Content-Type: application/json" -d "{\"fields\": {\"status\": \"success\"}}" "https://firestore.googleapis.com/v1/projects/transfer-status-realtime/databases/(default)/documents/transfers?&key=*** removed ***"
{
"error": {
"code": 400,
"message": "Invalid value at 'document.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), \"success\"",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "document.fields[0].value",
"description": "Invalid value at 'document.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), \"success\""
}
]
}
]
}
}
如果相关,则使用外部json文件解决相同问题
C:\WSs\reactive-stack\reactive-front\src>curl -X POST -H "Content-Type: application/json" -d @load-data-tofirestore.json "https://firestore.googleapis.com/v1/projects/transfer-status-realtime/databases/(default)/documents/transfers?&key=***removed"
{
"error": {
"code": 400,
"message": "Invalid value at 'document.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), \"success\"",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "document.fields[0].value",
"description": "Invalid value at 'document.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), \"success\""
}
]
}
]
}
}
{"fields": {"status": "success"}}
这是json文件
C:\WSs\reactive-stack\reactive-front\src>curl -X POST -H "Content-Type: application/json" -d @load-data-tofirestore.json "https://firestore.googleapis.com/v1/projects/transfer-status-realtime/databases/(default)/documents/transfers?&key=***removed"
{
"error": {
"code": 400,
"message": "Invalid value at 'document.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), \"success\"",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "document.fields[0].value",
"description": "Invalid value at 'document.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), \"success\""
}
]
}
]
}
}
{"fields": {"status": "success"}}
*解决方案*
从卷曲
C:\WSs\reactive-stack\reactive-front\src>curl -H "Content-Type: application/json" -X POST https://firestore.googleapis.com/v1/projects/transfer-status-realtime/databases/(default)/documents/transfers -d "{\"fields\": { \"status\": { \"stringValue\": \"outro status\" } }}"
{
"name": "projects/transfer-status-realtime/databases/(default)/documents/transfers/ddwHxjy7eQV3640pW0Xx",
"fields": {
"status": {
"stringValue": "outro status"
}
},
"createTime": "2020-04-06T16:21:16.453553Z",
"updateTime": "2020-04-06T16:21:16.453553Z"
}
来自PowerShell
PS C:\Users> $body = @"
>> {
>> "fields": {
>> "status": {
>> "stringValue": "fracasso"
>> }
>> }
>> }
>> "@
>>
PS C:\Users\Cast> $params = @{
>> Uri = 'https://firestore.googleapis.com/v1/projects/transfer-status-realtime/databases/(default)/documents/transfers'
>> Method = 'POST'
>> Body = $body
>> ContentType = 'application/json'
>> }
>>
PS C:\Users\Cast> Invoke-RestMethod @params
name fields createTime
---- ------ ----------
projects/transfer-status-realtime/databases/(default)/documents/transfers/z4TB6gzcgXMRUGR8ecek @{status=} 2020-04-06...
PS C:\Users>
我相信问题的根本原因是windows系统上的卷曲。如中所示,在windows shell中解析命令行参数的方式存在限制 而不是使用这个:
-d'{"fields": {"status": "success"}}'
您应该将您的有效负载声明为:
-d "{\"fields\": {\"status\": \"success\"}}"
另一种解决方案是将JSON写入文件,并在cURL查询中使用其路径。我相信您可以通过这种方式保持正确的JSON格式,但您将被迫使用一个文件,如果您不打算多次发送相同的有效负载,这远远不是最佳的:
-d @file_with_data.json
我得到了上面粘贴的“…”fieldViolations:“[{”field:“document.fields[0].value”,“description:”“document.fields[0].value'…”处的无效值。从Google Firestore的角度看,我似乎没有正确地编写命令,因为我看到了“Invalid value”“@JimC我无法理解最后一个错误,第二个问题的根本原因是什么?URL或JSON的内容?错误“…”type.googleapis.com/google.rpc.BadRequest,fieldViolations…”肯定是由错误的命令格式引起的。若您将上次成功的错误和所有其他失败的错误进行比较,我以前使用过“…{…”。。。“stringValue”…“在所有其他故障中,考虑到解决方案,您尝试过Cygwin吗?Cygwin终端遵循linux/bash转义规则:)(它基本上是Windows上GNU bash的一个端口)老实说,我对Cygwin了解不多,但我很快会尝试一下。对于一个非常虚拟的Cygwin,Cigwin终端和PowerShell之间有什么区别?它们的目的是相同的,对吗?在Windows终端上运行linux命令,对吗?不,PowerShell是关于运行…C-Sharp?-就像Windows上的Windows命令一样。PowerShell没有任何东西但是Cygwin是关于在Windows上运行Linux命令的。顺便说一句,当你有Cygwin的时候,也要保留安装程序。Cygwin安装程序基本上是你的包管理器,当你需要新的Linux程序时(比如
rsync
或其他什么),您需要重新运行Cygwin安装程序。它不会再次安装所有内容,只会添加所需的额外软件包