[英]Record from firebase query does not exist as expected
我正在寫一個異步 function async_unlike_post
,用於我的 firebase HTTP unlike_post
index.js
中表單的 unlike_post 。 async_unlike_post
的實現存放在另一個文件posts.js
索引.js
exports.unlike_post = functions
.https
.onCall(async_unlike_post);
帖子.js
const { admin, db } = require("../util/admin");
//admin.initializeApp(config);
//const db = admin.firestore();
const { uuid } = require("uuidv4");
const {
success_response,
error_response
} = require("../util/validators");
exports.async_unlike_post = async function(req, res) {
try {
const like_snapshot = await db
.collection('likes')
.where("liker_id", '==', req.liker_id)
.where('entity_id', '==', req.entity_id)
.limit(1).get();
if (!like_snapshot.exists) {
console.log("Case 1");
return error_response("Post has not been liked by user");
}
const post_document = db.doc(`/posts/${req.entity_id}`);
const post_snapshot = await post_document.get();
if (!post_snapshot.exists) {
console.log("Case 2");
return error_response("Post not found");
}
console.log("Should delete");
return db.collection("likes").doc(like_snapshot.id).delete();
} catch (error) {
return error_response(error);
}
}
mockLike = {
liker_id: "string1",
entity_id: "string2"
}
exports.async_unlike_post(mockLike);
運行node posts.js
后,我希望看到我的 Firestore 數據庫中的文檔被刪除。 但是,在我的控制台上,它說它轉到案例 1。我的 firestore 數據庫中的likes
集合中肯定有一個文檔具有以下屬性。
created_at: Some_timestamp,
entity_id: "string2",
liker_id: "string1",
type: "post"
有人能啟發我為什么我的代碼的執行會進入案例 1 嗎?
您的 function 接受res
和req
的參數:
function asyncUnlikePost(req, res)
但是,您正在導出一個 onCall function,它不提供相同的 arguments:
exports.unlike_post = functions
.https
.onCall(async_unlike_post);
正如您從callable functions 的文檔中看到的那樣,它提供了data
和context
參數,而不是 req 和 res。 onCall 函數不同於 onRequest 函數。 它們不“兼容”。
另外,這里的調用:
mockLike = {
liker_id: "string1",
entity_id: "string2"
}
exports.async_unlike_post(mockLike);
沒有傳遞所需的res
和req
參數。 您只是傳遞了一些 object。 那是行不通的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.