Firestore:查询集合中的所有文档

时间:2017-11-25 15:15:42

标签: android firebase-authentication google-cloud-firestore

我的Firestore结构如下所示:

-(coll)users
    -(doc)uniqueID  
    name
    email  
    (coll)clients
        -(doc)uniqueID
        clientName
        clientEmail  

我想要实现的目标如下:

  1. 用户通过FirebaseUI Auth flow登录
  2. 如果用户(uid从Auth恢复)在firestore db中不存在,我创建一个由他的uid命名的文档
  3. 一旦我有了uid,我运行一个查询来加载客户端集合,以便使用RecyclerView将它们显示到列表中(如果集合为空,因此用户尚未创建任何客户端,但我显示空列表屏幕)
  4. 我尝试根据documentation

    使用以下代码进行查询
        clientsCollection = db.collection(FIRESTORE_COLLECTION_USERS)
                .document(mUid)
                .collection(FIRESTORE_COLLECTION_CLIENTS);
    
        clientsCollection
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()){
                            for (DocumentSnapshot document: task.getResult()){
                                Log.d(LOG_TAG, document.getId() + " => " + document.getData());
                            }
                        } else {
                            Log.d(LOG_TAG, "error getting documents: ", task.getException());
                        }
                    }
                });
    

    我得到以下RuntimeException

    java.lang.NullPointerException: Provided document path must not be null.
    

    即使客户端集合中存在一些由唯一的uid命名的文档,我也会得到这个。

    感谢你能给我的任何线索! :)

1 个答案:

答案 0 :(得分:1)

运行第一个语句时,错误消息指示// tests/AppBundle/Repository/BotsDbTest.php <?php use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\Finder\Finder; use Symfony\Component\Config\FileLocator; use Symfony\Component\Yaml\Parser; class BotsDbTest extends KernelTestCase { private $doctr; private $db_cred; private $db; /** * {@inheritDoc} */ protected function setUp() { $kernel = self::bootKernel(); $this->doctr = $kernel->getContainer() ->get('doctrine') ->getManager(); // for tests with loaded content $this->db = new \AppBundle\Wiks\BotsDb(); // https://symfony.com/doc/current/bundles/extension.html // get DB credientals from "parameters_test.yml": $configDirectories = array( 'app/config' ); $locator = new FileLocator( $configDirectories ); $yamlUserFiles = $locator->locate( 'parameters_test.yml', null, false ); // https://davidegan.me/parse-yaml-in-php-using-symfony-yaml/ $yaml = new Parser(); $yaml_array = $yaml->parse( file_get_contents( $yamlUserFiles['0'] ) ); // needen DB is the second in Symfony - as database2 in file "parameters_test.yml": $prefix_db = 'database2'; // looking for all keys with suffix: eq: 'database2_host' $needed_sufix = [ 'host', 'port', 'name', 'user', 'password' ]; $this->db_cred = array(); foreach ( $yaml_array[ 'parameters' ] as $key => $value ) { if ( strpos( $key, $prefix_db ) !== false ) { foreach ( $needed_sufix as $needed_key ) { if ( strpos( $key, $needed_key ) !== false ) { $this->db_cred[ $needed_key ] = $value; } } } } if ( count( $this->db_cred ) == count( $needed_sufix ) ) { // check is all found /*Array ( [host] => 127.0.0.1 [port] => [name] => db_name [user] => user_name [password] => *** ) */ $finder = new Finder(); // find and put into mysql all files as prepared content to tests $finder->files()->name('*.sql'); foreach ( $finder->in( array( 'tests/dbcontent' ) ) as $file ) { $shell_command = 'mysql --user='.$this->db_cred['user'].' --password='.$this->db_cred['password']; $shell_command .= ' '.$this->db_cred['name'].'< '.$file->getRealPath(); shell_exec( $shell_command ); } } } /** * {@inheritDoc} */ protected function tearDown() { parent::tearDown(); // remoove DB content ( all tabels ): $shell_command = 'mysqldump --user='.$this->db_cred['user'].' --password='.$this->db_cred['password'].' '; $shell_command .= '--add-drop-table --no-data '.$this->db_cred['name'].' | '; $shell_command .= 'grep -e \'^DROP \| FOREIGN_KEY_CHECKS\' | '; $shell_command .= 'mysql --user='.$this->db_cred['user'].' --password='.$this->db_cred['password'].' '.$this->db_cred['name']; shell_exec( $shell_command ); $this->doctr->close(); $this->doctr = null; // avoid memory leaks } /** tests, tests, tests... * */ public function test_getBots() { $res = $this->db->getBots( $this->doctr ); $this->assertEquals(5, count( $res )); [...] mUid。这很可能意味着您在用户登录之前运行此代码。

确保您仅在用户登录后调用此代码,例如来自null

AuthStateListener.onAuthStateChanged()