【问题标题】:Firebase Querying?Firebase 查询?
【发布时间】:2020-08-19 11:01:08
【问题描述】:

我在我的 firebase 数据库中查询时遇到问题。我正在尝试从我的端点“.../api/user”获取经过身份验证的用户的所有数据。 这是我的代码中的路线:

// GET DATA OF USER
router.route("/user").get(FBAuth, getAuthenticatedUser);

这里我使用了一个中间件,它对令牌进行解码并将其设置在 req.user 中,当然还会验证用户是否经过身份验证:

// FBAuth middleware    
module.exports = (req, res, next) => {
      admin
        .auth()
        .verifyIdToken(idToken)
        .then((decodedToken) => {
          req.user = decodedToken;
          return db
            .collectionGroup("users")
            .where("idUser", "==", req.user.uid)
            .limit(1)
            .get();
        })
        .then((data) => {
          req.user.name = data.docs[0].data().name
          return next();
        })
        .catch((err) => {
          console.error("Error while verifying token", err);
          return res.status(403).json(err);
        });
    };

以上一切正常,但是在req.user设置成功后,我们转到函数“getAuthenticatedUser”,它不起作用:

//Controller
exports.getAuthenticatedUser = (req, res) => {
  let userData = {};

  db.collectionGroup("users")
    .where("email", "==", req.user.email) //".where("idUser", "==", req.user.uid)" nothing works here
    .limit(1)
    .get()
    .then((doc) => {
      if (doc.exists) {                   // Always goes to the else no matter what query I do
        userData.credentials = doc.data();
        return db
          .collection("comptes")
          .doc(req.user.name)
          .collection("courses")
          .get();
      }else{
        return res.status(400).json({email: 'Email not found => ' + req.user.email}); 
// the req.user.email does exist and contain the right email, and also exists in the database...
          }
        })
        .then((data) => {
          if (data.exists) {
            userData.courses= [];
            data.forEach((doc) => {
              userData.courses.push(doc.data());
            });
          }
          return res.json(userData);
        })
        .catch((err) => {
          console.error(err);
          return res.status(500).json({ error: err.code });
        });
    };

我看不到查询如何用于日志记录、中间件,但不适用于必须使用此设置的实际控制器,因为它是私有路由?

【问题讨论】:

  • 您是否为集合组创建了索引为docs suggest?您是否也尝试过使用硬编码电子邮件进行相同的查询?
  • 是的,是的,问题是“doc.exists”不起作用

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


【解决方案1】:

我终于修好了,如果有人有同样的问题,这里是解决方案:

exports.getAuthenticatedUser = (req, res) => {
  let userData = {};

  db.collectionGroup("users")
    .where("email", "==", req.user.email)
    .limit(1)
    .get()
    .then((doc) => {
      if (doc.docs[0].exists) { // <----- doc.docs contain a list of data
        userData.credentials = doc.docs[0].data();
        return db
          .collection("comptes")
          .doc(req.user.name)
          .collection("courses")
          .get();
      }
        })
        .then((data) => {
          if (data.exists) {
            userData.courses= [];
            data.forEach((doc) => {
              userData.courses.push(doc.data());
            });
          }
          return res.json(userData);
        })
        .catch((err) => {
          console.error(err);
          return res.status(500).json({ error: err.code });
        });
    };

【讨论】:

    猜你喜欢
    • 2018-04-12
    • 1970-01-01
    • 2021-11-17
    • 2017-02-09
    • 2021-02-09
    • 2017-01-30
    • 2017-11-25
    • 2018-03-05
    相关资源
    最近更新 更多