Firestore-查询,然后更新 [英] Firestore - Query, then update

查看:43
本文介绍了Firestore-查询,然后更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想返回具有特定ID的项目并对其执行更新操作.我的查询得到了正确的结果,但不允许我进行更新.

I want to return an item with a specific ID and perform an update operation on it. My query gets the right result, but it won't let me update it.

我尝试按照本教程进行操作: https://www.youtube.com/watch?v=mYyPbfpoZeM 并阅读文档.两者都没有帮助.相同主题的其他主题完全不同.

I tried following this tutorial: https://www.youtube.com/watch?v=mYyPbfpoZeM And read the documentation. Both didn't help. Other threads to the same topic are, well, different.

我有一个对象数据库,这些对象的唯一ID存储为整数.我有一个HTML表单来获取ID作为用户输入,并在下面的查询中检索相应的对象.

I have a database of objects which have a unique ID stored as integer. I have an HTML form to get an ID as user input and the query below to retrieve the according object.

我尝试了这个.该查询有效,但更新无效.

I tried this. The query worked, the update didn't.

db.collection('objects').where('ID','==', ID ).get().then((snapshot) => {
        snapshot.docs.forEach( doc => {
            console.log('debug');
            console.log(doc.data().ID);
        })
    });

我仍然对Firebase和js还是陌生的,所以如果我的代码完全错误,请原谅我.我目前对此感到困惑:

I am still new to firebase and js, so please forgive me if my code is uterly wrong. I am currently stuck with this:

db.collection('objects').where('ID','==', ID ).get().then((doc) => {
    console.table(doc);
});

哪些仍然无法正常工作.

Which is still not working.

对于第二个片段,我目前得到了一个难以辨认的表,在其中我无法真正找到想要的对象.

For the second snippet an I currently get an unlegible table in which I can't really find the object I was looking for.

如何在单个文档中更新单个值?

How do I update a single value in a single document?

我忘记了更新功能的实现尝试.我在片段1的for循环内尝试了 doc.update({value:0}),这产生了 doc.update不是函数.对于 doc.data().update(...).

I forgot my implementation attempts of the update function. I tried doc.update({value:0}) inside the for loop in snippet one which yielded doc.update is not a function. Similarly for doc.data().update(...).

在第二个片段中,我主要尝试查看返回的内容并运行上述uodate函数的变体.没有成功.

In the second snippet I mainly tried to see what I got returned and ran variations of the above mentioned uodate function. With no success.

推荐答案

我通过研究Firestore文档中有关实际功能的详细信息,设法使其能够正常工作.某种程度上,我很难找到它.

I managed to get it to work by studying the part of the firestore documentation that goes more into detail about the actual functions. Somehow it was hard for me to find this.

db.collection('objects').where('ID', '==', ID).limit(1).get().then((query) => {  
        const thing = query.docs[0];
        var currVal = thing.data().value;                                               
        const newVal = currVal - minus;
        thing.ref.update({value:newVal});
    });

因此,我使用 where 获取查询对象,使用 get 获取 then 承诺解析中的querySnapshot,使用docs [0] 来检索第一个(也是唯一的)documentSnapshot,最后是 ref 来获取一个引用,该引用使以后的整个事情可更新.

So I use where to get a Query object, use get to get a a querySnapshot inside the then promise resolve, use docs[0] to retrieve the first (and only) documentSnapshot and finally ref to get a reference that makes the whole thing updatable later.

这篇关于Firestore-查询,然后更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆