【问题标题】:Firestore Trouble with get() then add()使用 get() 然后 add() 的 Firestore 问题
【发布时间】:2020-05-12 19:53:23
【问题描述】:

Vue.js 和 Firestore 的新手,努力做一些我认为应该很简单的事情。

1) 我在我的 firestore 中查询一个现有的模板文档以检索一些默认值。

2) 我想使用这些默认值在不同的集合中创建一条新记录。

我可以控制台记录模板中的默认值并且它们正确显示(在下面的代码中,“console.log”显示了模板文档中当前的值,所以我相信以“this.”开头的变量应该现在有了我的正确值,可以在创建新文档时使用了,是吗?

但是当使用这些默认值运行后续的 .add() 时,新文档是在“节点”中创建的,但值不是在第一个查询中检索到的值,而是我最初将变量初始化为的值在 data() 中创建它们时,在这种情况下是空白字符串。 'nodes' 集合中新创建的文档具有正确的字段,但它们是空白字符串,仅显示没有值的“”。

如果重要的话,这个函数 createNewLocation() 是通过在页面上按下一个按钮来启动的。它似乎有效,我只是无法获取要复制的值。

我已经尝试了几种不同的方法。无论我做什么,我都无法使查询的返回值在我的其余代码中有用。这是否与创建新文档时未返回第一个查询的延迟有关?如果是这样如何解决这个问题?

谢谢。

<script>
import db from '../components/firebaseInit'
export default {
    name: 'loc-new',
    data () {
        return {
            //Values from entry form
            new_loc_id: null,
            new_loc_name: null,

            //Holders for default values used in location creation
            node_hw_ver: 0,
            node_sw_ver: 0,
            node_user_descr: '',
            node_user_name: '',
            node_type_class: '',
            node_type_id: 0,

        }
    },
    methods: {
        createNewLocation () { 
            //get template /templates/(nodes)/node_templates/
            db.collection('templates').doc('nodes').collection('node_templates')
                .where('use_for_new', '==', true).get().then((querySnapshot) => {
                querySnapshot.forEach((doc) => {
                    this.node_hw_ver = doc.data().default_hw_ver
                    console.log("hw ", this.node_hw_ver)
                    this.node_sw_ver = doc.data().default_sw_ver
                    console.log("sw ", this.node_sw_ver)
                    this.node_user_descr = doc.data().default_user_descr
                    console.log("descr ", this.node_user_descr)
                    this.node_user_name = doc.data().default_user_name
                    console.log("name ", this.node_user_name)
                    this.node_type_class = doc.data().type_class
                    console.log("class ", this.node_type_class)
                    this.node_type_id = doc.data().type_id
                    console.log("id ", this.node_type_id)
                })
            })
            db.collection('nodes').add({
                    loc_id: this.new_loc_id,
                    addr: 1,
                    user_name: this.node_user_name,
                    user_descr: this.node_user_descr,
                    type_id: this.node_type_id,
                    type_class: this.node_type_class
            })
            .then(function(docRef){
                //do nothing                
            })
            .catch(function(error){
                console.error("Error adding node document: ", error)
                return null
            })

        }
    }
}
</script>

【问题讨论】:

  • 您的意思是在查询的回调中添加文档inside,而不是在查询执行后立即,但在它完成之前?
  • @DougStevenson 是的,我想我需要在回调中添加它,但我不明白如何实际编写语法。我读过回调,但我无法理解它。我不太了解箭头函数和所有大括号等。从逻辑上讲,我了解我要做什么,只是不确定如何实际编写语法。我通常用 C 语言编写低级微处理器,这是我第一次尝试将 js 用于 Web 应用程序。我真的在努力兑现承诺。我只想要数据,并不关心我是如何得到它的。 :)

标签: javascript vue.js google-cloud-platform google-cloud-firestore


【解决方案1】:

我想我已经成功了。我知道它没有更新,因为承诺尚未返回。我对 js 的语法有点模糊(我通常在 C 中工作),所以这只是反复试验,可能不干净或不正确,但如果其他人偶然发现这篇文章,这确实有效。

我在第一个查询的 .then 中添加了 .add() 查询。我知道这可以使用“等待”来完成,但我无法理解我找到的各种示例中的语法。可能有一种更简洁的方法可以做到这一点,但它现在有效。

<script>
import db from '../components/firebaseInit'
export default {
    name: 'loc-new',
    data () {
        return {
            //Values from entry form
            new_loc_id: null,
            new_loc_name: null,

            //Holders for default values used in location creation
            node_hw_ver: 0,
            node_sw_ver: 0,
            node_user_descr: '',
            node_user_name: '',
            node_type_class: '',
            node_type_id: 0

        }
    },
    methods: {
        createNewLocation () { 

            //get template /templates/(nodes)/node_templates/
            db.collection('templates').doc('nodes').collection('node_templates')
                .where('use_for_new', '==', true).limit(1).get().then((querySnapshot) => {
                querySnapshot.forEach((doc) => {

                    this.node_hw_ver = doc.data().default_hw_ver
                    console.log("hw ", this.node_hw_ver)
                    this.node_sw_ver = doc.data().default_sw_ver
                    console.log("sw ", this.node_sw_ver)
                    this.node_user_descr = doc.data().default_user_descr
                    console.log("descr ", this.node_user_descr)
                    this.node_user_name = doc.data().default_user_name
                    console.log("name ", this.node_user_name)
                    this.node_type_class = doc.data().type_class
                    console.log("class ", this.node_type_class)
                    this.node_type_id = doc.data().type_id
                    console.log("id ", this.node_type_id)

                    db.collection('nodes').add({
                    loc_id: this.new_loc_id,
                    addr: 1,
                    user_name: this.node_user_name,
                    user_descr: this.node_user_descr,
                    type_id: this.node_type_id,
                    type_class: this.node_type_class
                        })
                        .then(function(docRef){
                            //do nothing                
                        })
                        .catch(function(error){
                            console.error("Error adding node document: ", error)
                            return null
                        })
                })

        })
    }
}
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多