更新不是功能-Firebase Firestore [英] update not a function - firebase firestore

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

问题描述

将数据发送到数据库后,我松了一口气,如果没有时间戳,就无法按时间顺序进行组织.因此,当我尝试下面的代码说更新不是功能时,我试图用时间戳更新我的数据.

I reaslied after sending data to my database that you cannot organised chronologically without the timestamp. I am therefore trying to update my data with the time stamp, when trying the code below it says that update is not a function.

我不确定将该更新功能放在何处以尝试将时间戳添加到Firestore中的数据.

I am unsure where to put this update function to try add the timestamp to the data in firestore.

function sendRate() {
    
        var selected = JSON.parse(window.sessionStorage.getItem("selected"));
        var answers = JSON.parse(window.sessionStorage.getItem("answers"));
    
        var firebaseConfig = {
          };
        // Initialize Firebase
        firebase.initializeApp(firebaseConfig);
        const db = firebase.firestore();
        db.collection("user").update({
            timestamp: firebase.firestore.FieldValue.serverTimestamp()
        })

        db.collection("user").doc("2afc").set(answers)
        db.collection("user").doc("rated").set(selected) 
            .then(function () {
                window.location.href =("postExperiment.html")
            })
            .catch(function (error) {
                console.error("Error writing document: ", error);
            })
    }
    
}

推荐答案

Firestore没有更新查询的概念,您可以在其中向服务器发送命令,然后将其应用于所有文档或所有符合条件的文档.

Firestore has no concept of update queries, where you send a command to the server and it then applies it to all documents or all documents matching a condition.

相反,您需要阅读文档,然后一次或分批更新它们.最简单的方法是:

Instead, you'll need to read the documents and then update them one at a time, or in batches. The simplest approach is:

db.collection("user").get().then((querySnapshot) => {
  querySnapshot.forEach((doc) => {
    doc.ref.update({
      timestamp: firebase.firestore.FieldValue.serverTimestamp()
    })
  })
})

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

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