我正在尝试将 JSON 文件上传到 Firebase 实时数据库,但不断收到错误 400。我已通过 Firebase 网站手动上传相同的 JSON 文件,并且上传已成功完成。
我检查了 firebase-debug.log 文件,注意到发送的 JSON 文件没有文件名,但我不确定为什么会这样。
[debug] [2024-07-11T18:49:42.872Z] >>> [apiv2][query] PUT dbpath.firebaseio.com/users/uid/works/.json
[debug] [2024-07-11T18:49:42.873Z] >>> [apiv2][body] PUT dbpath.firebaseio.com/users/uid/works/.json [stream]
[debug] [2024-07-11T18:49:43.291Z] <<< [apiv2][status] PUT dbpath.firebaseio.com/users/uid/works/.json 400
[debug] [2024-07-11T18:49:43.292Z] <<< [apiv2][body] PUT dbpath.firebaseio.com/users/uid/works/.json {"error":"Invalid data; couldn't parse JSON object, array, or value."}
[debug] HTTP Error: 400, Invalid data; couldn't parse JSON object, array, or value.
[error]
[error] Error: Unexpected error while setting data: FirebaseError: HTTP Error: 400, Invalid data; couldn't parse JSON object, array, or value.
这是我正在运行的一段 bash 代码(此代码在验证文件有效并且 var 和 uid 文件具有相同数量的用户数据后运行):
for ((i=0; i<${#uids[@]}; i++)); do
uid=${uids[$i]}
var=${vars[$i]}
varFile=$var
varFile=${var//[-]/_}
# Simular la llamada a ORCID y guardar el resultado en un archivo JSON
echo "Realizando solicitud a ORCID para ${var}"
echo $varFile
jsonFilePath="/Users/user/Documents/folder/outputs_json/prueba/works${varFile}.json"
curl -H 'Content-Type: application/orcid+json' -H 'Authorization: Bearer authkey' "https://pub.orcid.org/v3.0/${var}/works" -i -o "$jsonFilePath"
jsonFile="works${varFile}.json"
# Quitar las primeras 15 líneas del archivo JSON
tail -n +17 "$jsonFile" > "${jsonFile}.tmp" && mv "${jsonFile}.tmp" "$jsonFile"
# Actualizar Firebase con el UID
echo "Actualizando Firebase para el UID ${uid}"
echo $jsonFile
firebase database:set "/users/${uid}/works/" --data ${jsonFile} -P project-name
done
我很困惑,因为我已将 --data 值更改为完整文件名和不同的变量,但我仍然遇到相同的错误。我还通过打印来验证文件名和路径是否正确存储在变量中。任何帮助将非常感激!
编辑:这是一个示例 JSON 文件:
{
"last-modified-date" : {
"value" : 1685720662037
},
"group" : [ {
"last-modified-date" : {
"value" : 1685720662037
},
"external-ids" : {
"external-id" : [ {
"external-id-type" : "doi",
"external-id-value" : "10.1111/tpj.15599",
"external-id-normalized" : {
"value" : "10.1111/tpj.15599",
"transient" : true
},
"external-id-normalized-error" : null,
"external-id-url" : {
"value" : "https://doi.org/10.1111/tpj.15599"
},
"external-id-relationship" : "self"
} ]
},
"work-summary" : [ {
"put-code" : 136214894,
"created-date" : {
"value" : 1685720662037
},
"last-modified-date" : {
"value" : 1685720662037
},
"source" : {
"source-orcid" : null,
"source-client-id" : {
"uri" : "https://orcid.org/client/0000-0001-9884-1913",
"path" : "0000-0001-9884-1913",
"host" : "orcid.org"
},
"source-name" : {
"value" : "Crossref"
},
"assertion-origin-orcid" : null,
"assertion-origin-client-id" : null,
"assertion-origin-name" : null
},
"title" : {
"title" : {
"value" : "The small RNA‐mediated gene silencing machinery is required in Arabidopsis for stimulation of growth, systemic disease resistance, and suppression of the nitrile‐specifier gene NSP4 by Trichoderma atroviride"
},
"subtitle" : null,
"translated-title" : null
},
"external-ids" : {
"external-id" : [ {
"external-id-type" : "doi",
"external-id-value" : "10.1111/tpj.15599",
"external-id-normalized" : {
"value" : "10.1111/tpj.15599",
"transient" : true
},
"external-id-normalized-error" : null,
"external-id-url" : {
"value" : "https://doi.org/10.1111/tpj.15599"
},
"external-id-relationship" : "self"
} ]
},
"url" : {
"value" : "https://doi.org/10.1111/tpj.15599"
},
"type" : "journal-article",
"publication-date" : {
"year" : {
"value" : "2022"
},
"month" : {
"value" : "02"
},
"day" : null
},
"journal-title" : {
"value" : "The Plant Journal"
},
"visibility" : "public",
"path" : "/0000-0001-7872-3135/work/136214894",
"display-index" : "0"
} ]
},
],
"path" : "/0000-0001-7872-3135/works"
}
您使用的命令不正确:
firebase database:set "/users/${uid}/works/" --data ${jsonFile} -P project-name
--data
标志引入实际的 JSON 对象字符串作为下一个参数,而不是要导入的文件的名称。 您可以使用 firebase database:set --help
: 来了解 CLI 的期望
Usage: firebase database:set [options] <path> [infile]
store JSON data at the specified path via STDIN, arg, or file
Options:
-d, --data <data> specify escaped JSON directly
-f, --force pass this option to bypass confirmation prompt
--instance <instance> use the database <instance>.firebaseio.com (if omitted, use default database
instance)
--disable-triggers suppress any Cloud functions triggered by this operation
-h, --help output usage information
此帮助告诉您应该提供文件名作为其自己的参数(不带标志)。 所以你的命令应该更像这样:
firebase -P project-name database:set "/users/${uid}/works/" "${jsonFile}"