Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore - Query, then update

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.

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.

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);
        })
    });

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?

EDIT: 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(...).

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.

like image 659
Tharrry Avatar asked Apr 16 '19 18:04

Tharrry


People also ask

Is there any way to update a specific index from the array in firestore?

Is there any way to update a specific index from the array in Firestore? No, there is not! This is not possible because if you want to perform an update, you need to know the index of that particular element. When talking about Cloud Firestore arrays, the things are different that you might think.

What is onSnapshot in Firebase?

You can listen to a document with the onSnapshot() method. An initial call using the callback you provide creates a document snapshot immediately with the current contents of the single document. Then, each time the contents change, another call updates the document snapshot.


2 Answers

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("users").where("name", "==", somename).limit(1).get().then(query => {
                console.log(query);
                const thing = query.docs[0];
                console.log(thing.data());
                let tmp = thing.data();
                tmp.current_game_play = tmp.current_game_play + 1;
                console.log(tmp);
                thing.ref.update(tmp);
            });

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.

like image 100
Tharrry Avatar answered Sep 24 '22 16:09

Tharrry


try this:

var objectRef= db.collection("objects").doc(ID);

objectRef.update({
    value: 0
}).then(function() {
    console.log("Document successfully updated!");
}).catch(function(error) {
    // The document probably doesn't exist.
    console.error("Error updating document: ", error);
});
like image 30
Nadhir Falta Avatar answered Sep 21 '22 16:09

Nadhir Falta