admin管理员组

文章数量:1225550

I have a problem that's bugging me for days. I am trying to create a Firebase Cloud function that reads from the Firestore database.

My Firestore DB looks like this:

Problem is that I cannot list users like this:

db.collection('users').get().then((snapshot) => snapshot.forEach(...));

If I try to do this I get empty response, like there are no users in my users collection.

But I try to access user directly it works:

await db.collection('users/5CZxgu8nmNXu2TgplwOUdOIt8e33/receipts').get()

My complete code:

import * as functions from 'firebase-functions';

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.cat = functions.https.onRequest(async (req, res) => {
    const receiptList: any = [];
    const db: Firestore = admin.firestore();

    const usersRef = await db.collection('users').get();
    console.log(usersRef.empty);        // Returns true

    const receiptsRef = await db
        .collection('users/5CZxgu8nmNXu2TgplwOUdOIt8e33/receipts')
        .get();

    receiptsRef.forEach((receipt: any) => {
        console.log(receipt);
        receiptList.push(receipt);
        // Here I can access data
    });

    res.send(receiptList);
    return '';
});

Does anyone have any idea what I'm doing wrong? Thank you!

I have a problem that's bugging me for days. I am trying to create a Firebase Cloud function that reads from the Firestore database.

My Firestore DB looks like this:

Problem is that I cannot list users like this:

db.collection('users').get().then((snapshot) => snapshot.forEach(...));

If I try to do this I get empty response, like there are no users in my users collection.

But I try to access user directly it works:

await db.collection('users/5CZxgu8nmNXu2TgplwOUdOIt8e33/receipts').get()

My complete code:

import * as functions from 'firebase-functions';

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.cat = functions.https.onRequest(async (req, res) => {
    const receiptList: any = [];
    const db: Firestore = admin.firestore();

    const usersRef = await db.collection('users').get();
    console.log(usersRef.empty);        // Returns true

    const receiptsRef = await db
        .collection('users/5CZxgu8nmNXu2TgplwOUdOIt8e33/receipts')
        .get();

    receiptsRef.forEach((receipt: any) => {
        console.log(receipt);
        receiptList.push(receipt);
        // Here I can access data
    });

    res.send(receiptList);
    return '';
});

Does anyone have any idea what I'm doing wrong? Thank you!

Share Improve this question edited Jun 18, 2021 at 17:41 matox asked Sep 9, 2019 at 17:02 matoxmatox 98711 silver badges28 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

Your users collection is actually empty. See how the document IDs are shown in italics? That means there is not actually a document in its place, however, there are subcollections with documents organized underneath them.

When you query a collection, you only get the documents that are immediately within that collection. A query will not pick up documents organized in subcollections. In this respect, queries are said to be "shallow". As you've seen, you need to reach deeper into the subcollection to get its documents.

Bottom line is that the queries you're showing are doing exactly what they're supposed to do.

Thanks again Doug for your help.

I manage to solve my problem. Here is my complete solution.

import * as functions from 'firebase-functions';
import {
    Firestore
} from '@google-cloud/firestore';

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.cat = functions.https.onRequest(async (req, res) => {
    const receiptList: any = [];
    const db: Firestore = admin.firestore();

    const receipts = await db.collectionGroup('receipts').get();
    receipts.forEach((doc: any) => {
        console.log(doc.id, ' => ', doc.data());
        receiptList.push(doc.data());
    });
    res.send(receiptList);
    return '';
});

.get() gets all documents. In your case those documents are empty therefore .get() doesn't consider them.

The simplest solution that I found for this is to replace .get() with .listDocuments(). Now you could read each doc entry like you would a doc.

本文标签: javascriptFirestore Cloud Function empty collectionStack Overflow