Node.js 我如何不使用.then()从cloud firestore获取数据
我希望在不使用此处所示承诺的情况下获取数据: 在云功能控制台中,我不会立即获取数据Node.js 我如何不使用.then()从cloud firestore获取数据,node.js,firebase,google-cloud-firestore,Node.js,Firebase,Google Cloud Firestore,我希望在不使用此处所示承诺的情况下获取数据: 在云功能控制台中,我不会立即获取数据 docRef.get().then(doc => { console.log("In Then"); console.log('Authorized User Data From Function:', doc.data()); result = doc.data(); // if (!doc.exists) { // c
docRef.get().then(doc => {
console.log("In Then");
console.log('Authorized User Data From Function:', doc.data());
result = doc.data();
// if (!doc.exists) {
// console.log('No such document!');
// } else {
// console.log('Payment Request Data:', doc.data());
// }
}).catch(err => {
console.log("In Catch");
console.log('Error getting document', err);
return false;
});
// I don't want to use this. This is taking time to complete
数据从云Firestore异步加载。无法立即从基于云的数据库获取数据。如果您想使用最现代的web API,就必须习惯使用这种异步API 在现代Node.js环境中使用
async
/wait
最接近于不使用then()
。使用async
/wait
时,上述代码如下所示:
doc = await docRef.get();
console.log('Authorized User Data From Function:', doc.data());
result = doc.data();
但请记住:虽然这可能在一开始读起来更自然,但在引擎盖下,它仍然在做完全相同的事情