【发布时间】:2021-04-11 09:37:07
【问题描述】:
我正在尝试创建一个系统,如果用户请求优惠券
- 它将检查用户是否已经请求了优惠券
- 那么它应该在一个集合中作为一个文档。
- 如果没有文档,它将生成一个文档。
所以我期望的是,如果我运行代码并且集合中有一个文档,它应该取消操作吗?
但事实并非如此。它只是更新文档字段 createdAt 和 qrCodeId。
export const addCoupon = functions.https.onCall(async (data, context) => {
const key = nanoid();
const today = admin.firestore.FieldValue.serverTimestamp();
const uid = context.auth!.uid;
const couponRef = db
.collection("kuponger")
.doc("newCoupons")
.collection(data.school)
.doc(uid);
const doc = await couponRef.get();
if (!context.auth) {
throw new functions.https.HttpsError(
"failed-precondition",
"Du måste vara inloggad"
);
}
if (!doc.exists) {
couponRef
.set({
createdAt: today,
createdBy: uid,
qrCodeId: key,
})
.catch((error) => {
throw new functions.https.HttpsError("unknown", error);
});
} else {
throw new functions.https.HttpsError("already-exists", "finns redan");
}
});
它看起来应该可以工作,但是当我尝试运行它并且集合中已经存在一个文档时,它会更新文档而不是取消操作。
你能帮帮我吗?
如果您需要更多信息,请发表评论。
【问题讨论】:
标签: node.js firebase google-cloud-firestore google-cloud-functions