Firestore/Javascript:是否可以同时更新旧文档和设置新文档(一次Javascript执行)?

Firestore/Javascript:是否可以同时更新旧文档和设置新文档(一次Javascript执行)?,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我试图完成的是点击一个按钮,将执行两个文档操作。第一个操作将更新旧的文档,在这种情况下,更新名为2019年11月30日的文档,并将其名为isActive的字段之一更改为false。第二个操作将设置一个新的文档,命名为用户执行所述操作的当前日期 let userRef1 = firebase.firestore().collection("users").doc(userId).collection("goal").orderBy("dateAdded", "desc").limit(1);

我试图完成的是点击一个按钮,
将执行两个文档操作。第一个操作将更新旧的
文档
,在这种情况下,更新名为2019年11月30日的
文档
,并将其名为
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,
                                        });
目前,上述行动都不起作用。但第二个操作在添加此代码之前生效(这应该是更新旧文档字段的第一个操作代码):

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,
                                        });
                                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"; 
                                }
数据库的图像:
当执行整个操作时,包围数据库中的包围字段将被更新。然后,将创建一个名为/id的新文档(当前日期)。

尽管我尝试了Doug Stevenson的答案。我仍然很难实现它。不过,我确实找到了一个解决办法,在单击按钮时使用
setTimeout()
,第一次查询将运行。而第二个将在执行前暂停3秒

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

虽然这似乎是非正统的,但我仍然认为它是可行的。如果您在执行
事务
批处理
时遇到困难,希望这对您有所帮助

要同时编写两个文档,您应该查看事务和批处理写入。您也不需要在
旁边等待
。然后()。相反,用try-catch块包围表达式
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,
                                        });