Klo*_*oot 9 typescript google-cloud-firestore
谁能帮我用 async/await 在 Typescript 中“翻译”这个例子
console.log("start")
var citiesRef = db.collection('cities');
var allCities = citiesRef.get()
.then(snapshot => {
snapshot.forEach(doc => {
console.log(doc.id, '=>', doc.data().name);
});
console.log("end")
})
.catch(err => {
console.log('Error getting documents', err);
});
Run Code Online (Sandbox Code Playgroud)
我测试了一些代码,但我认为我在“forEach”循环上做错了。
我想要在控制台中的结果:
start
Key1 => city1
Key2 => city2
end
Run Code Online (Sandbox Code Playgroud)
结果我参加了一些测试:
start
end
Key1 => city1
Key2 => city2
Run Code Online (Sandbox Code Playgroud)
提前谢谢
Tit*_*mir 12
在不知道类型的情况下,我根据它们的用法假设它们符合以下接口:
var db: {
collection(name: 'cities'): {
get(): Promise<Array<{
id: string;
data(): { name: string }
}>>
}
};
Run Code Online (Sandbox Code Playgroud)
鉴于该声明,async/await代码的一个版本将是
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20677 次 |
| 最近记录: |