首页 > 解决方案 > 在云 Firestore 和 php 中查询

问题描述

我想在 Firestore 中进行查询,但收到此错误

Undefined method 'orderBy'.

我的编码是

$collectionReference = $db->collection('Test3')->document('User1');
$query = $collectionReference->orderBy('Name')->limit(1);
$snapshot = $query->snapshot();

为什么会这样?

标签: phpfirebasegoogle-cloud-firestoreget

解决方案


$collectionReference的不是 CollectionReference;这是一个文档参考(因此是->document('User1'))。

->orderBy适用于 COLLECTION 查询。 如果您尝试获取需要使用的“Test3”集合:

$collectionReference = $db->collection('Test3');
$query = $collectionReference->orderBy('Name')->limit(1);
$snapshot = $query->snapshot();

没有看到->get()- 我不再使用 PHP,所以我不知道它是否隐含在语法中。


推荐阅读