Firestore / Javascript:是否可以同时更新一个旧文档并设置一个新文档(一个javascript执行程序?)

问题描述 投票:0回答:1

我想完成的是单击一个按钮,<script>将执行2个文档操作。第一个动作将更新旧的document,在本例中为document,名为2019年11月30日,并将其名为isActive的字段更改为false。第二个操作将设置一个新的document,命名为用户执行该操作的当前日期。

目前,上述操作均无效。但是,在添加此代码之前,第二个操作正在工作(这应该是更新旧文档字段的第一个操作代码)

let userRef1 = firebase.firestore().collection("users").doc(userId).collection("goal").orderBy("dateAdded", "desc").limit(1);
                                            return userRef1.get()
                                            .then(function(querySnapshot) {
                                                querySnapshot.forEach(function(doc) {
                                                    console.log(doc.id, " => ", doc.data());
                                                    this.getDateBeforeUpdate = doc.id; //this get the document ID or the date in this case (Nov 30, 2019)
                                            });
                                        })

                                    await firebase.firestore().collection("users").doc(userId).collection("goal").doc(getDateBeforeUpdate).update({
                                        'isActive': false,
                                        });

这是完成这两个操作的完整代码:

                                let userRef1 = firebase.firestore().collection("users").doc(userId).collection("goal").orderBy("dateAdded", "desc").limit(1);
                                        return userRef1.get()
                                        .then(function(querySnapshot) {
                                            querySnapshot.forEach(function(doc) {
                                                console.log(doc.id, " => ", doc.data());
                                                this.getDateBeforeUpdate = doc.id; //this get the document ID or the date in this case (Nov 30, 2019)
                                        });
                                    })

                                await firebase.firestore().collection("users").doc(userId).collection("goal").doc(getDateBeforeUpdate).update({
                                    'isActive': false,
                                    });

                                if( getDateAndConsent.getGoalDate.getTime() >= date1.addDays().getTime() ){
                                     if(getDateAndConsent.getYN == "Yes"){
                                        await firebase.firestore().collection("users").doc(userId).collection("goal").doc(americanDate).set({
                                            'startRange': startRange, 'endRange': endRange, 'dateAdded': data.dateAdded, 
                                            'isActive': data.isActive, 'doesmaintainBG': data.doesMaintainBG, 'goalDate': firebase.firestore.Timestamp.fromDate(data.goalDate),
                                            'goalWeight': data.inputWeight, 'goalForWeight': data.goalForWeight,
                                        })
                                        .then(function(){
                                            window.alert("Weight goal updated!");
                                            window.location.href = "diabetesManagement.php"; 
                                        })
                                        .catch(function(error){
                                            console.log("Error updating weight goal: ", error);
                                            window.alert("Error updating weight goal: " + error);
                                        })
                                    }
                                    else if(getDateAndConsent.getYN == "No"){
                                        await firebase.firestore().collection("users").doc(userId).collection("goal").doc(americanDate).set({
                                            'dateAdded': data.dateAdded, 'isActive': data.isActive, 
                                            'doesmaintainBG': data.doesMaintainBG, 'goalDate': firebase.firestore.Timestamp.fromDate(data.goalDate),
                                            'goalWeight': data.inputWeight, 'goalForWeight': data.goalForWeight,
                                        })
                                        .then(function(){
                                            window.alert("Weight goal updated!");
                                            window.location.href = "diabetesManagement.php"; 
                                        })
                                        .catch(function(error){
                                            console.log("Error updating weight goal: ", error);
                                            window.alert("Error updating weight goal: " + error);
                                        });
                                    }
                                    else{
                                        window.alert("error");
                                    }
                                }
                                else{
                                    window.alert("error date: you can only input dates three weeks from now");
                                    window.location.href = "diabetesManagement.php"; 
                                }

数据库图像:enter image description here当执行整个动作时,被包围的数据库中被包围的字段将被更新。然后,将创建一个新文件,命名为/标识当前日期。

javascript firebase google-cloud-firestore
1个回答
0
投票

尽管我尝试了道格·史蒂文森的答案。我仍然很难实施它。我确实找到了解决方法,尽管通过单击按钮时使用setTimeout()会运行第一个查询。第二个将在执行之前暂停3秒钟。

代码如下:

                                if( getDateAndConsent.getGoalDate.getTime() >= date1.addDays().getTime() ){
                                     if(getDateAndConsent.getYN == "Yes"){
                                        firebase.firestore().collection("users").doc(userId).collection("goal").where("isActive", "==", true)
                                        .get()
                                        .then(function(querySnapshot) {
                                            querySnapshot.forEach(function(doc) {
                                                console.log(doc.id, " => ", doc.data());
                                                this.getDateBeforeUpdate = doc.id; //this get the document ID or the date in this case (Nov 30, 2019)
                                                firebase.firestore().collection("users").doc(userId).collection("goal").doc(getDateBeforeUpdate).update({
                                                    'isActive': false,
                                                })
                                            })
                                        })
                                        setTimeout(function(){firebase.firestore().collection("users").doc(userId).collection("goal").doc(americanDate).set({
                                            'startRange': startRange, 'endRange': endRange, 'dateAdded': data.dateAdded, 
                                            'isActive': true, 'doesmaintainBG': data.doesMaintainBG, 'goalDate': firebase.firestore.Timestamp.fromDate(data.goalDate),
                                            'goalWeight': data.inputWeight, 'goalForWeight': data.goalForWeight,
                                        })
                                        .then(function(){
                                            window.alert("Weight goal updated!");
                                            window.location.href = "diabetesManagement.php"; 
                                        })
                                        .catch(function(error){
                                            console.log("Error updating weight goal: ", error);
                                            window.alert("Error updating weight goal: " + error);
                                        })}, 3000);
                                    }

尽管这看起来是非常规的,但我仍然认为这是一种解决方法。如果您很难实现transactionsbatch,希望这对您有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.