简体   繁体   English

Firebase Firestore REST 示例

[英]Firebase Firestore REST example

Hello I am looking to write a script which uses firebase firestore and writes some json to a specific collection in firestore.您好,我想编写一个脚本,该脚本使用 firebase firestore 并将一些 json 写入 firestore 中的特定集合。 I have done this with the realtime db but firestore is a tad different below is my Realtime db snippet that works.我已经使用实时数据库完成了此操作,但 firestore 有点不同,下面是我的实时数据库片段。

curl -X POST \
-d '{"param1":"'"$1"'", "param2":"'"$2"'"}' \
https://xxxx.firebaseio.com/xxxx.json?

Thanks for the help谢谢您的帮助

After Reading the documentation I got to this阅读文档后,我得到了这个

curl -X POST \
-H "Content-Type: application/json" \
-d'{
"fields": {
"Field1": {
"stringValue": "'"$var1"'"
},
"Field2": {
"stringValue": "'"$var2"'"
},
"Field3": {
"stringValue": "$var3"
}
}
}'\"https://firestore.googleapis.com/v1beta1/projects/**PROJECT_ID**/databases/(default)/documents/**COLLECTION_ID**?&key=(YOUR API KEY)"

The accepted answer helped me, but it took me a long time to figure out how can I use data types other than stringValues , so I am adding this answer hoping someone finds this helpful in the future.接受的答案对我有帮助,但我花了很长时间才弄清楚如何使用stringValues以外的数据类型,所以我添加了这个答案,希望将来有人发现这有帮助。

curl -X POST \
-H "Content-Type: application/json" \
-d' {
    "fields": {
        "Field1": {
            "arrayValue": {
                "values": [{
                    "mapValue": {
                        "fields": {
                            "key1": {
                                "stringValue": "val1"
                            },
                            "key2": {
                                "stringValue": "val2"
                            }
                        }

                    }
                }]
            }
        },
        "Field2": {
            "integerValue": <intValue>
        },
        "Field3": {
            "stringValue": "var3"
        }
    }
}'\"https://firestore.googleapis.com/v1beta1/projects/**PROJECT_ID**/databases/(default)/documents/**COLLECTION_ID**?&key=<YOUR WEB API KEY>"

Use this for reference. 以此作为参考。

example field value示例字段值

var data = { "fields": { "productName":{"stringValue": dealname.toString()}, "companyname":{"stringValue": companyName.toString()}, "contact":{"stringValue": contact.toString()}, "email":{"stringValue": email.toString()}, "domain":{"stringValue": domain.toString()}, "createdate":{"stringValue": createdate.toString()}, "salesCode":{"stringValue": code.toString()}, "price":{"stringValue": amount.toString()}, "phone":{"stringValue": phone.toString()}, "orderId":{"stringValue": orderId.toString() } } };

more information firestore information更多信息Firestore 信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM