且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firebase Firestore get() 异步/等待

更新时间:2023-01-03 14:15:25

在不知道类型的情况下,我根据它们的用法假设它们符合以下接口:

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

var db: {
    collection(name: 'cities'): {
        get(): Promise<Array<{
            id: string;
            data(): { name: string }
        }>>
    }
};

鉴于该声明,代码的 async/await 版本将是

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