【问题标题】:how to add document to firestore database using react native如何使用本机反应将文档添加到firestore数据库
【发布时间】:2018-06-19 07:40:10
【问题描述】:

我们正在尝试使用 react native 应用手动将文档值添加到 firestore 数据库。

这是我们为网络找到的参考代码

db.collection("cities").doc("LA").set({
    name: "Los Angeles",
    state: "CA",
    country: "USA"
})
.then(function() {
    console.log("Document successfully written!");
})
.catch(function(error) {
    console.error("Error writing document: ", error);
});

对于本机反应,我们使用了这段代码

firebase.firestore().collection('users').doc("hello").add({
        title: this.state.textInput,
        complete: false,
      })

我收到错误,例如添加我们未定义

如何向其中添加文档和集合?

【问题讨论】:

    标签: javascript firebase react-native google-cloud-firestore


    【解决方案1】:

    您可以将文档添加到集合中。您不能将文档添加到文档中。

    要将文档添加到您的 users 集合中:

    firebase.firestore().collection('users').add({
      title: this.state.textInput,
      complete: false,
    })
    

    所以从那里删除.doc("hello")

    【讨论】:

    • 我们的结构是这样的,在集合下我们有文档,然后是集合
    • 好的,那么在这种情况下,您的文档下有一个子集合,需要识别子集合:firebase.firestore().collection('users').doc("hello").collection("SUBCOLLECTION").add({
    【解决方案2】:

    在这里我找到了我的数据结构的解决方案

    firebase
      .firestore()
      .collection("Users")
      .doc("mydoc")
      .collection("Activities")
      .doc("Database")
      .set({
        key: "1",
        value: "",
      })
      .then((ref) => { console.log(ref) });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 2021-01-18
      相关资源
      最近更新 更多