【问题标题】:What is the suitable query for the following case?以下情况的合适查询是什么?
【发布时间】:2017-12-15 05:08:51
【问题描述】:

这是我的数据库结构:

我正在尝试列出“区域设置”等于“埃及开罗”的所有用户,因此我进行了以下查询:

exports.calculateMatches = functions.https.onRequest((request, response) => {

// 访问位于请求用户区域设置中的用户配置文件

databaseRef.child("users").orderByChild("locale").equalTo(request.query.locale).once("value")
     .then(snap => {
       snap.forEach(profile => {
         console.log(profile);
       });
    });
 });

请注意,此功能已部署到 Firebase 云功能,这是我在日志中得到的:

【问题讨论】:

  • 与其包含错误截图,不如将文本复制并直接放在问题中以便更容易搜索。

标签: firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

HTTPS 类型函数要求您向客户端发送send a response 才能终止函数。否则,它们将始终超时,并且客户端将一直等待。

例如:

const databaseRef = admin.database().ref('')
exports.calculateMatches = functions.https.onRequest((request, response) => {
    databaseRef.child("users").orderByChild("locale").equalTo(request.query.locale).once("value")
    .then(snap => {
        const profiles = []
        snap.forEach(profile => {
            profiles.push(profile.val())
        });
        response.send(profiles)
    })
    .catch(error => {
        response.status(500).send(error)
    });
});

【讨论】:

  • 太棒了,现在我对 JSON 数据有了一个不错的看法,但我什么也没得到。我试图将其保留为仅 orderByChild 而没有 equalTo 部分,但也一无所获……查询有问题吗?
  • /users 没有名为“locale”的子项。那里有几个额外级别的孩子。您必须重组数据以适应您想要执行的查询类型。 stackoverflow.com/questions/35446695/…
猜你喜欢
  • 1970-01-01
  • 2018-06-01
  • 2010-10-10
  • 1970-01-01
  • 2020-09-25
  • 2019-02-07
  • 2011-03-01
  • 2012-06-08
  • 1970-01-01
相关资源
最近更新 更多