Firebase功能-查询Firestore [英] Firebase function - query firestore

查看:63
本文介绍了Firebase功能-查询Firestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Cloud函数中的Firestore检索一些数据,但什么也没收回.客户端上的相同查询为我提供了正确的结果.它可能很小,但我看不到这个问题.我在做什么错了?

Im trying to retrieve some data from firestore within a cloud function, but get nothing back. The same query on the client-side gives me the correct results. It's probably something small but I don't see the issue. What am I doing wrong?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
db.settings({ timestampsInSnapshots: true });

exports.myFunction = functions.https.onCall((data, context) => {
  const info = getInfo();
  //do some stuff with the info
  return info;
}

function getInfo() {
  const query = db
    .collection('info')
    .where('table_nr', '==', 1)
    .where('number', '<=', 25)
    .orderBy('number', 'desc')
    .limit(1);

  const info = query.get().then(snapshot => {
    snapshot.forEach(doc => {
      return doc.data();
    })
  })
  return info;
}

当我调用此函数时,我得到:"data:null"

When I make a call to this function I get: "data: null"

let info = functions.httpsCallable('myFunction')

info().then(res => { console.log(res) })

我尝试了许多不同的选项,例如将最后一部分更改为:

I tried a lot of different options, like when I change the last part to:

const info = query.get().then(snapshot => {
  snapshot.docs;
})

我得到一个带有1个对象的数组.因此,我确定查询中有一个包含数据的文档. console.log给了我:

I get an array with 1 object. So I'm sure there is a document in the query with data. The console.log gives me:

{data: Array(1)}
data: Array(1)
0: {_ref: {…}, _fieldsProto: {…}, _serializer: {…}, _validator: {…}, 
_readTime: {…}, …}
length: 1
__proto__: Array(0)
__proto__: Object

并且:

return query.get().then(querySnapshot => {
  if (querySnapshot.empty) {
    return { exists: false }
  } else {
    return { exists: true }
  }
})

console.log:

The console.log:

{data: {…}}
data:
  exists: true
  __proto__: Object
  __proto__: Object

补充一点,我为查询创建了一个(有效的)索引.

Mabye good to add that I created an (working) index for the query.

推荐答案

在两种情况下,您都将返回对象的承诺,该承诺不是您真正想要发送给客户端的.编写可调用对象时,您需要返回一个承诺,该承诺可以解析为要发送的 exact JavaScript对象.您不能只退还任何东西.您需要做的就是将querySnapshot转换成简单的旧JavaScript对象,这些对象描述了您希望客户端知道的内容. querySnapshot对象本身不能序列化-它是一个复杂的对象,描述了有关查询结果的许多内容.

In both cases, you're returning a promise for an object that isn't what you really want to send to the client. When you write a callable, you need to return a promise that resolves to the exact JavaScript object that you want to send. You can't just return anything. What you'll have to do is convert that querySnapshot into plain old JavaScript objects that describe what you want the client to know. A querySnapshot object itself is not serializable - it is a complex object that describes many things about the query results.

首先定义以下内容:您希望客户收到什么确切?定义实际的JavaScript对象的外观.现在,将查询结果转换成这样.至少,您可以将整个文档集作为纯JS对象发送,如下所示:

First define this: What exactly do you want the client to receive? Define what the actual JavaScript object should look like. Now, convert the query results to look like that. At a minimum, you can send the entire set of documents as plain JS objects like this:

return query.get().then(querySnapshot => {
    return querySnapshot.docs.map(doc => doc.data());
})

这会将带有原始文档对象的数组返回给客户端.但是我不清楚您要发送的是什么(因为您没有定义期望).但这是一个开始.

This will return an array to the client with raw document objects. But it's not clear to me that's what you want to send (since you didn't define your expectations). But it's a start.

这篇关于Firebase功能-查询Firestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆