首页 > 解决方案 > 在 firestore 中使用 whereGreaterThanOrEqualTo 时出错

问题描述

使用 firebase 的 whereGreaterThanOrEqualTo 时出现错误。我想要做的是搜索我的数据库,如果它以相应的字母开头,这是我的代码:

constructor(fireStore, rootStore) {
this._firestore = fireStore;
this._employeeInfo = this._firestore.collection('Employee');
}

getEmployeeInfo = async () => {
const employeeList = [];
const snapshot = await this._employeeInfo
    .whereGreaterThanOrEqualTo("fullName", this.fullName)
    .get()

snapshot.docs.forEach((doc) => {
    employeeList.push(doc.data());
});

return employeeList;
};

错误是

"TypeError: _this2._employeeInfo.whereGreaterThanOrEqualTo is not a function. (In '_this2._employeeInfo.whereGreaterThanOrEqualTo("fullName", _this2.fullName)', '_this2._employeeInfo.whereGreaterThanOrEqualTo' is undefined)"

标签: javascriptgoogle-cloud-firestore

解决方案


您正在使用 Javascript,因此您需要以这种方式使用它:

this._employeeInfo.where("fullName", '>=', this.fullName)

以下是可用比较运算符的列表以及不同语言的示例:https ://firebase.google.com/docs/firestore/query-data/queries#query_operators

如文档中所述:仅“对于 iOS、Android 和 Java,比较运算符在方法中显式命名。”

旁注:如果您尝试模拟一种自动完成样式,您最好使用https://firebase.google.com/docs/reference/js/firebase.database.Query#startat而不是进行字符串比较。

像这样的东西应该工作:

this._employeeInfo.orderBy("fullName").startAt(searchText).endAt(searchText + "\uf8ff");

推荐阅读