【问题标题】:How to get userId from Firestore on Node.js backend?如何从 Node.js 后端的 Firestore 获取 userId?
【发布时间】:2022-01-16 01:30:58
【问题描述】:

我是 firebase 新手,在我的应用上设置用户个人资料路由时遇到问题。我试图做到这一点,以便可以检索存储在 Firestore 中的 userId,并且该特定用户发布的所有帖子都将显示在用户个人资料页面上。请任何帮助将不胜感激!

这是我的 Firestore 数据库:

这是我目前所拥有的,但它没有显示与用户 ID 关联的所有帖子

const express = require("express");
const router = express.Router();
const firestore = require("firebase/firestore");
const db = firestore.getFirestore();

router.get("/", (req, res) => {
  res.send("No user id provided");
});

router.get("/:uid", (req, res) => {
  const uid = req.params.uid;
  const userPost = firestore.getDoc(firestore.doc(db, "reviews", uid));

  userPost(uid, req.query && req.query.dev === "true")
    .then((user) => {
      return res.send(user);
    })
    .catch((e) => {
      return res.send(e);
    });
});

module.exports = router;

【问题讨论】:

    标签: node.js firebase google-cloud-firestore


    【解决方案1】:

    这一行在/reviews/:uid 获得了单个(可能不存在)帖子:

    const userPost = firestore.getDoc(firestore.doc(db, "reviews", uid));
    

    您要做的是为集合 /reviews where docData.userId = uid 中的文档构建一个查询。

    const reviewsColRef = firestore.collection(db, "reviews");
    const userPostsQuery = firestore.query(reviewsColRef, firestore.where("userId", "==", uid));
    const userPostsPromise = firestore.getDocs(userPostsQuery);
    
    userPostsPromise
      .then((querySnapshot) => {
        const postsArray = querySnapshot.docs.map(docSnap => {
          const docData = { id: docSnap.id, ...docSnap.data() };
          delete docData.userId; // it's the same for every post, may as well omit it
          delete docData.email; // same as above
          return docData; 
        });
        res.json({ posts: postsArray, count: postsArray.length });
      })
      .catch((err) => {
        // don't send the full error object to the client,
        // instead you should log the error and send the
        // client as little information as possible
        console.error(`Failed to get user posts for firebase:${uid}`, err);
        res.status(500).json({ error: err.code || err.message }); // Don't forget to use an appropriate status code!
      });
    

    注意事项:

    • 我建议使用解构来导入 Firestore 库。将其导入为 firestore 会抵消模块化 SDK 仅导入您需要的内容的好处。
    import { getFirstore, query, collection, ... } from "firebase/firestore";
    
    • 如果此代码在您控制的服务器上运行,请考虑改用 Firebase Admin SDK,因为这可以让您绕过安全规则并放宽速率限制。
    import { getFirestore, query, collection, ... } from "firebase-admin/firestore";
    
    • 附带说明,如果从 SDK 导入的名称与您想在其他地方使用的名称冲突,您可以重命名它。您可能会在同时使用 RTDB 和存储时看到这一点,因为它们都导出 ref
    import { getDatabase, ref: rtdbRef } from "firebase/database";
    import { getStorage, ref: storageRef } from "firebase/storage";
    
    const userDataRef = rtdbRef(getDatabase(), "users", uid, "data");
    const imgStorageRef = storageRef(getStorage(), "path/to/image.png");
    
    • 将电子邮件视为类似于电话号码的敏感数据。没有人喜欢垃圾邮件,限制从数据库中提取电子邮件的方法是一种很好的做法。除非您正在为公司网络开发内部工具,否则您应该尽可能隐藏用户的电子邮件。
    • 电子邮件地址不应与评论一起存储。而是为每个用户保留一个包含文档的集合(例如,/userProfile/:uid 的文档)并限制特权用户的访问。
    • 考虑在用户不存在时添加404 Not Found 错误。
    • 考虑添加身份验证以限制对 API 的访问。

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 2018-07-23
      相关资源
      最近更新 更多