Firebase Firestore get() async/await

后端 未结 1 561
离开以前
离开以前 2020-12-10 16:37

can anyone help me to \"translate\" this example in Typescript with async/await

console.log(\"start\") 
var citiesRef = db.collection(\'cities\');
var allCi         


        
1条回答
  •  爱一瞬间的悲伤
    2020-12-10 17:09

    Without knowing the types, I assumed base on their usage that they conform to the following interface:

    var db: {
        collection(name: 'cities'): {
            get(): Promise>
        }
    };
    

    Given that declaration, an async/await version of the code would be

    async function foo() {
        console.log("start")
        var citiesRef = db.collection('cities');
        try {
            var allCitiesSnapShot = await citiesRef.get();
            allCitiesSnapShot.forEach(doc => {
                console.log(doc.id, '=>', doc.data().name);
            });
            console.log("end")
        }
        catch (err) {
            console.log('Error getting documents', err);
        }
    }
    

    0 讨论(0)
提交回复
热议问题