【发布时间】:2020-12-28 22:31:09
【问题描述】:
我有一个功能,当我单击按钮时会触发该功能。根据 docId,我可以在 Firestore 中检索正确的下载 URL。但我也想更新我的“客户”集合中的特定字段(availableDownloads)。我无法让它工作。
此代码运行良好。它返回下载地址。
exports.getDownloadUrl = functions.https.onCall(async(data, context) => {
var docRef= await db.collection('projects').doc(data.docId);
return docRef.get().then(function(doc){
const downloadURL = doc.data().downloadURL;
return downloadURL;
}).catch(function(error) {
// Handle error
});
});
然而事实并非如此。它返回 null
exports.getDownloadUrl = functions.https.onCall(async(data, context) => {
var docRef= await db.collection('projects').doc(data.docId);
return docRef.get()
.then(async function(doc){
const downloadURL = doc.data().downloadURL;
const userRef = await db.collection('customers').doc(context.auth.uid);
return userRef.update({
availableDownloads: admin.firestore.FieldValue.increment(-1)
}).then(()=> {
return downloadURL;
}).catch((error)=> {
})
}).catch(function(error) {
// Handle error
});
});
【问题讨论】:
-
最好不要将 async/await 与 then/catch 混合使用。如果您能够在任何地方使用 async/await,请在任何地方使用它来简化您的代码。还可以考虑添加日志记录以了解此代码的实际执行方式。我们在这里看不到您的数据或任何变量。
-
好的,谢谢,我可能会这样做。
-
另外:这里不需要
awaitawait db.collection('customers').doc(context.auth.uid)。 -
是的,谢谢,我注意到了。感谢您的反馈。
标签: javascript node.js firebase google-cloud-firestore google-cloud-functions