首页 > 解决方案 > Firebase 函数中的 Firestore 查询始终为空

问题描述

我有一个 Firebase 函数,它获取数据并将其用于查询 Firestore。查询始终返回 null,即使数据库中有等效数据。

我已经尝试对 id 进行硬编码,但它仍然返回 null,所以我确定问题不在于查询中使用的数据。

查询:

/**
 * Returns a user
 * @param {string} userId the user id.
 * @return {any} The user.
 */
async function getUser(userId: string): Promise<any> {
  const userResponse =
      await admin.firestore().collection("users")
          .where("id", "==", userId)
          .limit(1)
          .get();
  if (userResponse.docs.length > 0) {
    return userResponse.docs[0];
  } else {
    return null;
  }
}

它被称为:

/**
 * Deletes the user account
 */
export const someFunction = functions.https.onCall(
    async (data, context) => {
      if (!context.auth) {
        return {status: "error", code: 401, message: "Not signed in"};
      }

      try {
        const firestore = admin.firestore();
        const userId = data.userId.trim();
        const userData = await getUser(userId);
        functions.logger.info(
            "User data: ",
            userData
        );

        ...

数据结构如下

数据结构

已经坚持了很长一段时间了,所以非常需要任何帮助。提前致谢

标签: typescriptfirebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


推荐阅读